节点googleapis:在短时间内获取错误:invalid_request

时间:2017-06-23 09:28:44

标签: node.js youtube google-api youtube-api youtube-data-api

我正在使用节点googleapis库向youtube数据api发出请求。我开始使用passport passport-youtube-v3库对用户进行身份验证。一切正常。我能够进行身份验证,并且可以向youtube数据api发出授权请求。但经过一段时间(大约1-2小时)后,凭据似乎已过期或只是变为无效,我收到以下错误:

{ Error: Invalid Credentials
    at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:85:15)
    at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
    at IncomingMessage.g (events.js:286:16)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:926:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  code: 401,
  errors: 
   [ { domain: 'global',
       reason: 'authError',
       message: 'Invalid Credentials',
       locationType: 'header',
       location: 'Authorization' } ] }

我知道auth令牌会在一段时间后过期。但根据the documentation,这不应该在6个月不活动之前发生。此外,我没有提出过多的可能导致令牌无效的请求。不过我尝试实现一个手动刷新访问令牌,如下所示:

const fetch = (user, ressource, operation, opts) => {
  let oauth2Client = new OAuth2();
  let client = google.youtube({ version: 'v3',auth: oauth2Client })[ressource][operation];

  oauth2Client.credentials = {
      access_token: user.youtube.token,
      refresh_token: user.youtube.refreshToken
  };

  return new Promise((success, failure) => {
    client(opts, function(err, data, response) {
        if (err)
          if(err.code === 401)
            oauth2Client.refreshAccessToken(function(err, tokens) {
              console.log(err);
            });
          else
            failure(err);
        if (data)
          success(data);
    });
  });
}

我不确定我是否正确地实现了它,如果这样做甚至是有道理的。我收到以下错误:

{ Error: invalid_request
    at Request._callback (/Users/flavio/Code/BA/node_modules/google-auth-library/lib/transporters.js:81:15)
    at Request.self.callback (/Users/flavio/Code/BA/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/Users/flavio/Code/BA/node_modules/request/request.js:1091:12)
    at IncomingMessage.g (events.js:286:16)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:926:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9) code: 400 }

收到第一个错误时还有什么问题?访问令牌是否可能如此快速到期?如果是这样,我该如何正确刷新它?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

似乎问题是我没有正确地实例化OAuth2。我没有在创建时将我的应用程序的凭据传递给构造函数。一切似乎现在都有效。

let oauth2Client = new OAuth2(
  clientID,
  clientSecret,
  callbackURL
);

我仍然唯一的困惑是,在它开始抛出上述错误之前,为什么它对几个请求起作用。