Node.js EveryAuth获取Google电子邮件地址

时间:2011-10-31 23:10:26

标签: node.js oauth everyauth

我正在尝试使用EveryAuth NPM lib对Google进行OAuth2.0调用时检索谷歌电子邮件地址。有没有人设法使用EveryAuth回复电子邮件?

everyauth.google
  .entryPath('/auth/google')
  .callbackPath('/auth/google/callback')
  .appId('216450162097.apps.googleusercontent.com')
  .appSecret('8b6yf2nznWHgAu7iKNyGn-0F')
  .scope(['https://www.googleapis.com/auth/userinfo.email'])
  .findOrCreateUser( function(session, userAttributes) {
    console.log(userAttributes);  })
  .redirectPath('/'); 

范围:https://www.googleapis.com/auth/userinfo.email导致异常:

错误: 错误 401 (未找到)!! 1 display:block; height:55px; margin:0 0 -7px; width:150px} *> #g {margin-left:-2px} #g img {visibility:hidden} * html #g img {visibility:visible} * + html #g img {visibility:visible}        Google

401 的 那是一个错误。

您的请求中存在错误。 这就是我们所知道的。

    at [object Object] .fail(/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/lib/promise.js:50:15)     在EventEmitter。 (/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/lib/modules/google.js:58:15)     在EventEmitter.emit(events.js:67:17)     在EventEmitter._respond(/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/node_modules/restler/lib/restler.js:127:12)     在EventEmitter._fireEvents(/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/node_modules/restler/lib/restler.js:131:52)     at /Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/node_modules/restler/lib/restler.js:115:19     在IncomingMessage。 (/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/node_modules/restler/lib/restler.js:205:5)     在IncomingMessage。 (/Users/thegoleffect/Documents/Projects/Spoondate/nitrous/node_modules/everyauth/node_modules/restler/lib/restler.js:113:32)     在IncomingMessage.emit(events.js:81:20)     在HTTPParser.onMessageComplete(http.js:133:23)

1 个答案:

答案 0 :(得分:1)

根据google,范围和api终点略有不同,这让我有点困惑。如果您对google.js进行了以下更改,则Google Auth2.0 api将返回该用户的电子邮件地址。

  .fetchOAuthUser( function (accessToken) {
    var promise = this.Promise();
    rest.get('https://www.googleapis.com/userinfo/email', {
      query: { oauth_token: accessToken, alt: 'json' }
    }).on('success', function (data, res) {
      console.log(data);
      var oauthUser = { email: data };
      promise.fulfill(oauthUser);
    }).on('error', function (data, res) {
      console.log(data);
      promise.fail(data);
    });
    return promise;
  });