1
我正在使用節點googleapis
庫向youtube數據api發出請求。我開始使用passport
passport-youtube-v3
庫對用戶進行身份驗證。一切正常。我能夠進行身份驗證,並且可以向YouTube數據API提出授權請求。但一定時間後(約1-2小時)的憑據似乎已經過期或者只是變得無效,並且我收到以下錯誤:節點googleapis:在短時間內獲取錯誤:invalid_request
{ 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' } ] }
據我所知,身份驗證令牌一段時間後到期。但根據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 }
獲得第一個錯誤時還有什麼問題?訪問令牌可能過期如此迅速嗎?如果是這樣,我該如何正確刷新它?
任何幫助,非常感謝!
我不確定是什麼問題,因爲你正在使用'refresh_token',如果它失敗了,這意味着'refresh_token'已經無效了。您是否在[授權應用列表](https://myaccount.google.com/permissions)中撤銷了該應用?如果您的上一個問題是這種情況,那麼我發佈了一個用戶存儲一次的答案,'refresh_token'的有效性未經過測試,所以如果您使用的是相同的方法,那麼如果用戶已經撤銷了令牌手動。 –
您可以嘗試使用[本示例](https://gist.github.com/bertrandmartel/64bf4bf1df8a4ba63301cfa202163c90)(我使用[passport-google-auth](https://github.com/jaredhanson/passport-google-oauth )改爲) –
感謝您的幫助!它看起來像是埃爾斯的錯誤。 – Flavio