问题描述
使用Google的开发者控制台:
- 我已经创建了一个项目,并为该项目创建了一个客户端.
- 我已经激活了Youtube API数据
- 我将回调设置为 http://localhost:3000/callback
- 我已经下载了"client_secrets".
- I have created a project and created a client for that project.
- I have activated Youtube API Data
- I set a the callback to http://localhost:3000/callback
- I have downloaded the "client_secrets".
节点服务器的初始步骤:
- 使用"client_secrets"创建了一个名为oauth2Client的新OAuth2Client实例
- 我使用oauth2Client变量生成了URL,并指定了以下内容:
var url = oauth2Client.generateAuthUrl({
access_type: 'offline', //returns fresh token,
scope: 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtubepartner'
});
Google授权页面:
- 我转到生成的URL,然后单击授权并重定向到 http://localhost:3000/callback
节点服务器的最终步骤:
- 我在URL中获得了代码"(看起来像这样: 4/gZpLEwZWD6OVEE7F5uXXXXXXXXXXXXXXXXX )
- 使用相同的oauth2Client变量,我试图像这样获取令牌:
oauth2Client.getToken(code, function (err, tokens) {
// set tokens to the client
console.log('errors ' + err);
console.log('tokens ' + tokens);
oauth2Client.setCredentials(tokens);
})
console.log的结果是:
The result from the console.log's are:
errors Error: invalid_request
tokens null
问题
是什么原因导致invalid_request
?我错过了哪一部分?
Question
What is causing the invalid_request
? What part did I miss?
其他一些类似的问题提出了不同的解决方案
Some other similar questions propose different solutions
关于这个主题,似乎有很多问题,但是大多数都是特定的问题,答案往往含糊不清.我在问一个通用的问题,并寻找一个具体的答案.
There seem to be quite a few questions on this subject, but most are specific questions with too often very vague answers.I'm asking a generic question and looking for a specific answer.
推荐答案
您没有提到oauth2Client实例的构造,因此在GutHub google-api-nodejs-client/issues/231
You didn't mention constructing of oauth2Client instance, so it can be a problem from lib issue on GutHub google-api-nodejs-client/issues/231
即,构造函数OAuth2中的第三个参数将是字符串
Namely, the third argument in constructor OAuth2 is going be a string
client_secrets.web.redirect_uris[0]
var oauth2Client = new OAuth2Client(
client_secrets.web.client_id,
client_secrets.web.client_secret,
client_secrets.web.redirect_uris[0] //<-- take first uri
);
这篇关于错误:invalid_request getToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!