Google OAuth - 无法在授权时读取未定义的 属性“0”
Google OAuth - Cannot read property '0' of undefined at authorize
我正在尝试使用 Google API 访问其他人的 public 日历。
我的代码 - 取自此页面: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 的值):
λ 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)
上面的 JSON 来自我在 运行 上面提到的步骤 URL 之后下载(并保存为 'client_secret.json')的文件。那么为什么缺少重定向 URL?也许这是我在 运行 设置时遗漏的东西?此外,说明暗示我正在为自己的日历设置一个 API,而我想做一个 API 来阅读别人的日历。他们将它显示在网页上,所以它是 public。 (参见我发布的相关问题:)
您能否按以下方式检索 client_secret.json
并再次尝试 运行 脚本?
- 单击 "Create credentials" 作为 OAuth 客户端 ID。
- 勾选"Other"。
- 由此,自动设置重定向 url。 url 可用于快速入门。
- 输入姓名。
- 点击"Create"。
- 点击"OK"。
- 下载JSON.
- 将 JSON 文件重命名为
client_secret.json
。
注:
在您 运行 使用新的脚本 client_secret.json
之前,请删除现有的 calendar-nodejs-quickstart.json
。当使用默认的 Quickstart 时,文件会在主目录的 .credentials
中创建。
如果这对您没有用,我很抱歉。
我正在尝试使用 Google API 访问其他人的 public 日历。
我的代码 - 取自此页面: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 的值):
λ 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)
上面的 JSON 来自我在 运行 上面提到的步骤 URL 之后下载(并保存为 'client_secret.json')的文件。那么为什么缺少重定向 URL?也许这是我在 运行 设置时遗漏的东西?此外,说明暗示我正在为自己的日历设置一个 API,而我想做一个 API 来阅读别人的日历。他们将它显示在网页上,所以它是 public。 (参见我发布的相关问题:
您能否按以下方式检索 client_secret.json
并再次尝试 运行 脚本?
- 单击 "Create credentials" 作为 OAuth 客户端 ID。
- 勾选"Other"。
- 由此,自动设置重定向 url。 url 可用于快速入门。
- 输入姓名。
- 点击"Create"。
- 点击"OK"。
- 下载JSON.
- 将 JSON 文件重命名为
client_secret.json
。
注:
在您 运行 使用新的脚本 client_secret.json
之前,请删除现有的 calendar-nodejs-quickstart.json
。当使用默认的 Quickstart 时,文件会在主目录的 .credentials
中创建。
如果这对您没有用,我很抱歉。