2014-05-04 102 views
3

我想讓谷歌加認證工作的Web應用程序,但不斷得到redirect_uri_mismatch錯誤。我已經檢查並重新檢查了在代碼和Google開發設置中匹配的重定向網址。google api redirect_uri_mismatch錯誤

我舉報了,事情是打破了位置 - > **崩潰這裏

任何幫助,將不勝感激。

JS結合

window.gplusSigninCallback = function(authResult) { 
    if (!authResult.code) { 
    return; 
    } 

    request 
    .post('/api/users') 
    .send({ code: authResult.code }) 
    .end(function(err, res) { 
     dom('#signinButton').remove(); 
    }); 
}; 

谷歌+組分

<span id="signinButton"> 
    <span class="g-signin" 
    data-scope="https://www.googleapis.com/auth/plus.login" 
    data-clientid="{clientId}" 
    data-redirecturi="postmessage" 
    data-accesstype="offline" 
    data-cookiepolicy="single_host_origin" 
    data-callback="gplusSigninCallback"> 
    </span> 
</span> 

POST /用戶

使用的代碼從https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js

'use strict'; 

const router = require('express').Router(); 
const User  = require('../models/user'); 

const GoogleCreds = require('../config/google_api_credentials').web; 
const googleapis = require('googleapis'); 
const OAuth2Client = googleapis.OAuth2Client; 

module.exports = router; 

/** 
* 
*/ 
router.post('/', function(req, res) { 
    /* jshint camelcase:false */ 

    const clientId  = GoogleCreds.client_id; 
    const clientSecret = GoogleCreds.client_secret; 
    const redirectUrl = 'http://localhost:3000'; 
    const oneTimeCode = req.body.code; 

    try { 
    authenticate(); 
    } catch (ex) { 
    console.log(ex.message); 
    } 

    function authenticate() { 
    googleapis 
     .discover('plus', 'v1') 
     .execute(function(err, client) { 
     let oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl); 

     getAccessToken(oauth2Client, function() { 
      getUserProfile(client, oauth2Client, 'me', function(err, profile) { 
      if (err) { 
       throw new Error(err); 
      } 

      let userAttrs = { 
       foo: 'bar' 
      }; 

      User.findOrCreate(userAttrs, function(err, user) { 
       res.set('X-API-TOKEN', user.apiToken); 
       res.send(user); 
      }); 
      }); 
     }); 
     }); 
    } 

    function getUserProfile(client, authClient, userId, callback) { 
    client 
     .plus.people.get({ userId: userId }) 
     .withAuthClient(authClient) 
     .execute(callback); 
    } 

    function getAccessToken(oauth2Client, callback) { 
    oauth2Client.generateAuthUrl({ 
     access_type: 'offline', // will return a refresh token 
     scope: 'https://www.googleapis.com/auth/plus.login' 
    }); 

    oauth2Client.getToken(oneTimeCode, function(err, tokens) { 
     if (err) { 
     // ** CRASHES HERE 
     throw new Error(err); 
     } 
     oauth2Client.setCredentials(tokens); 
     callback(); 
    }); 
    } 
}); 
+0

這似乎是一些好的可能的解決方案: http://stackoverflow.com/questions/13221978/getting-error-redirect-uri-mismatch-the-redirect-uri-in-the-request- http-loc – user3590396

回答

4

問題的原因是服務器端的redirectUrl也需要設置爲postmessage。

+0

「有趣」的是,如果你使用js API獲取臨時令牌(不是登錄按鈕,只是gapi.auth.signIn),那麼設置客戶端重定向uri是完全隱藏的。所以我只花了大約4小時試圖找出爲什麼我不斷收到redirect_uri_mismatch錯誤。非常感謝你。 – Kicsi

相關問題