如何解决为什么我无法获得Google Oauth Api访问令牌?
这是我的代码:
index.js:
const {google} = require('googleapis');
const fs = require('fs');
const readline = require('readline');
// generate a url that asks permissions for Drive scope
const scopes = ['https://www.googleapis.com/auth/drive'];
fs.readFile('credentials.json',(err,content) => {
if (err) return console.log('Error loading client secret file:',err);
// Authorize a client with credentials,then call the Google Drive API.
authorize(JSON.parse(content));
});
function authorize(credentials) {
const {client_secret,client_id,redirect_uris} = credentials.web;
const oAuth2Client = new google.auth.OAuth2(
client_id,client_secret,redirect_uris[0]);
url(oAuth2Client);
}
function url(oAuth2Client) {
const url = oAuth2Client.generateAuthUrl({
// 'online' (default) or 'offline' (gets refresh_token)
access_type: 'offline',// If you only need one scope you can pass it as a string
scope: scopes
});
oAuth2Client.getToken((code),token) => {
if (err) return console.error('Error retrieving access token',err);
oAuth2Client.setCredentials(token);
console.log(oAuth2Client);
})
console.log(url);
}
credentials.json:
{"web":{"client_id":"***.apps.googleusercontent.com","project_id":"***","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"_***","redirect_uris":["https://vast-stream-39133.herokuapp.com","http://localhost"],"javascript_origins":["https://vast-stream-39133.herokuapp.com","http://localhost"]}}
我有一个由url()函数创建的url,在使用该url发出请求后,我得到了类似的东西:
看起来不错,但是当我尝试在代码之后复制字符块并将其粘贴为我的身份验证代码以获取访问令牌时,它不起作用。值得注意的是,我正在使用已验证的oauth Web应用程序凭据来获取我的信息。
我认为我的重定向uri可能存在问题,因为当我使用桌面应用程序凭据执行相同的过程时,系统会自动为我提供一个uri,它将我带到需要的身份验证代码(如果将其放入oAuth2Client变量中) ,就可以准确地更新所需的内容,而不必从url中获取它(这可能是错误的处理方式)。我在这里做错了什么?感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。