首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >invalid_grant Google OAuth

invalid_grant Google OAuth
EN

Stack Overflow用户
提问于 2015-03-15 10:50:46
回答 1查看 855关注 0票数 1

我正试图通过Google的OAuth进行身份验证,但我在建立与他们API的连接时遇到了问题

我的客户代码:

代码语言:javascript
复制
'click #addChannel': function (event) {
    event.preventDefault();

    var userId = Meteor.userId();
    var options = {
      requestPermissions: [
        'https://www.googleapis.com/auth/youtube',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/youtube.force-ssl',
        'https://www.googleapis.com/auth/youtube.readonly',
        'https://www.googleapis.com/auth/youtube.upload',
        'https://www.googleapis.com/auth/youtubepartner',
        'https://www.googleapis.com/auth/youtubepartner-channel-audit',
      ],
      requestOfflineToken: true
    };

    Google.requestCredential(options, function(token) {
      Meteor.call('userAddOauthCredentials', userId, token, function(error, result) {
        if (error) {
          throw error;
        }
        console.log(result);
      });

    });

我的服务器代码:

代码语言:javascript
复制
userAddOauthCredentials: function(userId, token) {
    check(userId, String);
    check(token, String);

    var config = ServiceConfiguration.configurations.findOne({service: 'google'});
    if (!config) {
      throw new ServiceConfiguration.ConfigError();
    }

    console.log(token, config);

    var endpoint = 'https://accounts.google.com/o/oauth2/token';
    var params   = {
      code: token,
      client_id: config.clientId,
      client_secret: OAuth.openSecret(config.secret),
      redirect_uri: OAuth._redirectUri('google', config),
      grant_type: 'authorization_code',
    };

    try { <------------------------------------------------------ this fails
      response = HTTP.post(endpoint, { params: params });
    } catch (err) {
      throw _.extend(new Error("(first) Failed to complete OAuth handshake with Google. " + err.message),
                     {response: err.response});
    }

    if (response.data.error) { // if the http response was a json object with an error attribute
      throw new Error("(second) Failed to complete OAuth handshake with Google. " + response.data);
    } else {
      return {
        accessToken: response.data.access_token,
        refreshToken: response.data.refresh_token,
        expiresIn: response.data.expires_in,
        idToken: response.data.id_token
      };
    }

以上抛出一个[400] { "error" : "invalid_grant" }错误。

上面的大部分代码来自meteor帐户-google包在用户中的日志记录(在我的应用程序中运行得很好)。链接到:

server.js

对从这里开始有什么建议吗?

非常感谢

UPDATE1:

我在日志里看到了这些警告

代码语言:javascript
复制
W20150318-09:11:42.532(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.532(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.533(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.534(1) (oauth_server.js:398) Error in OAuth Server: Match error: Expected string, got undefined
EN

回答 1

Stack Overflow用户

发布于 2016-07-22 06:59:34

您必须将您的application/x-www-form-urlencoded. var params解析为请找到下面的代码来解析,就像我在php中所做的那样

代码语言:javascript
复制
   $fields_string="";
    foreach($params as $key=>$value) 
    { 
         $fields_string .= $key.'='.$value.'&';
    }
    rtrim($fields_string, '&');

现在,$filed_string将包含对params数组的解析。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29059781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档