Google OAuth - 无法在授权时读取未定义的属性'0' [英] Google OAuth - Cannot read property '0' of undefined at authorize
问题描述
我正尝试使用Google API访问其他人的公开日历。
我的代码 - 取自此页面: https://developers.google.com/google-apps/calendar/quickstart/nodejs
function authorize(credentials,callback){
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
console.log(credentials.installed =+
JSON.stringify(credentials.installed));
var redirectUrl = credentials.installed.redirect_uris [0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId,clientSecret,redirectUrl);
错误和控制台输出(为了安全性而更改了JSON的值):
λ节点testGoogleCalendarAPI.js
credentials.installed = {client_id:123412341234-whatever.apps.googleusercontent.com,project_id: 应用程序名称-187323, auth_uri: https://accounts.google.com/o/oauth2/auth, token_uri:https://accounts.google.com/o/oauth2/token ,auth_provider_x509_cert_url:https://www.googleapis.com/oauth2/v1/certs,client_secret:whatever}
E:\GitHub\NealWalters\ RabbiJoseph\testGoogleCalendarAPI.js:62
var redirectUrl = credentials.installed.redirect_uris [0];
^
TypeError:在授权时无法读取未定义
的属性'0'(E:\GitHub\NealWalters\RabbiJoseph\testGoogleCalendarAPI.js:62:56 )
at processClientSecrets(E:\GitHub\NealWalters\RabbiJoseph\testGoogleCalendarAPI.js:49:3)
at FSReqWrap.readFileAfterClose [as oncomplete](fs.js:447:3)
上面的JSON来自我下载的文件(并保存为'client_secret.json')运行上述引用的URL上的步骤。那么为什么重定向网址会丢失?也许这是我运行该设置时遗漏的东西?此外,这些说明暗示着我正在为自己的日历设置API,因为我想通过API来读取其他人的日历。他们将它显示在网页上,因此它是公开的。 (请参阅我发布的相关问题: 您可以像下面那样检索 在使用新的 如果这不起作用对你而言,我很抱歉。 I'm trying to use the Google API to access someone else's public calendar. My Code - taken from this page: https://developers.google.com/google-apps/calendar/quickstart/nodejs Error and console output (values of JSON changed for security): The JSON above comes from a file that I downloaded (and saved as 'client_secret.json') after running the steps on the above referenced URL. So why are the redirect URLs missing? Maybe that's something I left out when I ran that setup? Further, the instructions were implying that I was setting up an API for my own calendar, where as I want to do an API to read someone else's calendar. They have it displayed in on a web page, so it is public. (See related question I posted: Can I use Google Calendar API v3 to access someone else's public calendar without auth?) Can you retrieve
Before you run the script using new If this was not useful for you, I'm sorry. 这篇关于Google OAuth - 无法在授权时读取未定义的属性'0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! client_secret.json
并尝试再次运行该脚本吗?
注意:
client_secret.json
运行脚本之前,请删除现有的 calendar-nodejs-quickstart.json
。当使用默认的Quickstart时,该文件创建在home目录的 .credentials
中。
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
console.log ("credentials.installed=" +
JSON.stringify(credentials.installed));
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
λ node testGoogleCalendarAPI.js
credentials.installed={"client_id":"123412341234-whatever.apps.googleusercontent.com","project_id":"app-name-187323","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"whatever"}
E:\GitHub\NealWalters\RabbiJoseph\testGoogleCalendarAPI.js:62
var redirectUrl = credentials.installed.redirect_uris[0];
^
TypeError: Cannot read property '0' of undefined
at authorize (E:\GitHub\NealWalters\RabbiJoseph\testGoogleCalendarAPI.js:62:56)
at processClientSecrets (E:\GitHub\NealWalters\RabbiJoseph\testGoogleCalendarAPI.js:49:3)
at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:447:3)
client_secret.json
as following and try to run the script, again?
client_secret.json
.Note :
client_secret.json
, please remove the existing calendar-nodejs-quickstart.json
. When the default Quickstart is used, the file is created in .credentials
of homedirectory.