【问题标题】:Making an 'Authorized Request' to a Google API using node.js (with everyauth authentication)使用 node.js 向 Google API 发出“授权请求”(使用everyauth 身份验证)
【发布时间】:2012-08-25 02:28:28
【问题描述】:

我在 node.js 上有一个应用程序,使用everyauth 身份验证返回表单的令牌

{ access_token: 'ya29.AHES6ZTlm9qdoFuY9sdpdmFTJISGaQ_69LnW8fszSzPjSCs',
  token_secret:
    { token_type: 'Bearer',
     expires_in: 3600,
     id_token: 'eyJhbGciOisdUzI1NiJ9.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiaWQ
iOiIxMTQ0MDU3NTM5MTg1NTk1Mzc3NzciLCJhdWQiOiI0NDcwOTkyNjgzMjAuYXBwcy5nb29nbGV1c2V
yY29udGVudC5jb20iLCJjaWQiOiI0NDcwOTkyNjgzMjAuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20
iLCJ0b2tlbl9oYXNoIjoicEFXMDBIaUdaSWNRaWMtM09TLU9vQSIsImlhdCI6MTM0NjMyMzQwMiwiZXh
wIjoxMzQ2MzI3MzAyfQ.HUAhg2hdBUB2n1oUFW_9MOxAyr2O7u8GFkShxggTL2o2tBpwIi0_jLIuD1ri
mGkZwdlR5DwjODe4w2w2rLzPpb3YPCeh19zWg7pxP0huqvuVl3cPLXOkWxke46WIH9KNSQbV3oX34GA4
jTvzEV2-HCR_-GhDeG245FTSpxYpTnE',
     refresh_token: '1/Ns-89zzHPbIJlFYXAOCn4dKqxklN4Wc0Og-Gga5XSJA' } }

此时我该如何创建“authorized request”(获取文件列表)?

我尝试过以这种形式使用 node-auth:

googleOAuth = new OAuth("https://accounts.google.com/o/oauth2/auth",
            "https://accounts.google.com/o/oauth2/token", 
            config.clientID, config.secret, 
            "1.0A", undefined, "HMAC-SHA1");

 googleOAuth.get('https://www.googleapis.com/drive/v2/files/list', token.access_token, token.token_secret.id_token,  function (error, data) {
  if(error){ 
    console.log(error)
  }else{ 
    console.log(data)
  }
}); 

在我点击 google 提示中的“允许访问”后,googleOAuth.get 会记录错误

{"error":{"statusCode":401,"data":"{\n \"error\": {\n  \"errors\": [\n   {\n    \"domain\": \"global\",\n    \"reason\": \"authError\",\n    \"message\": \"Invalid Credentials\",\n    \"locationType\": \"header\",\n    \"location\": \"Authorization\"\n   }\n  ],\n  \"code\": 401,\n  \"message\": \"Invalid Credentials\"\n }\n}\n"}}

我的get 请求应该通过什么身份验证才能获得有效的文件列表?

使用两个身份验证库似乎很奇怪。不过,我不确定如何使用everyauth 发出签名请求。它似乎仅用于“登录”服务。

【问题讨论】:

    标签: node.js oauth-2.0 everyauth


    【解决方案1】:

    1) 在我的everyauth 注册用户功能中,我更新了范围以包括谷歌驱动器。

    exports.register = function register (everyauth, conf, users) {
      everyauth['google']
        .appId(conf.google.clientID)
        .appSecret(conf.google.secret)
        .scope('https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/userinfo.profile') // ADDED ADDITIONAL SCOPE VALUE HERE
        .findOrCreateUser(function (sess, token, accessTokenExtra, googleUserData) {
          var user = users.find('google', token);
          if (user) {
            return user;
          } else {
            return users.add('google', sess.id, token, accessTokenExtra, googleUserData);
          }
        })
        .redirectPath('/');
    };
    

    2) 在我的请求中,我放弃了 node-oauth 并使用请求,并将访问令牌作为参数。

    // was googleOAuth.get('http.... etc
    request('https://www.googleapis.com/drive/v2/files?access_token= + token.access_token,  function (error, response, body) {
        if(error){
          callback(error, null);
        }else{
          callback(null, data);
        }
    });
    

    3) 此时我返回的是 403 错误,而不是 401 错误。

    缺少的操作是根据此问题https://stackoverflow.com/a/10370391/1408316的答案在我的控制面板中设置 Google Drive API 和 Google Drive SDK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      • 1970-01-01
      • 2016-05-25
      • 2023-03-18
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多