Google OAuth - 无法在授权时读取未定义的属性'0' [英] Google OAuth - Cannot read property '0' of undefined at authorize

查看:174
本文介绍了Google OAuth - 无法在授权时读取未定义的属性'0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用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来读取其他人的日历。他们将它显示在网页上,因此它是公开的。 (请参阅我发布的相关问题:

解决方案我可以使用Google Calendar API v3访问其他人的公共日历吗?

您可以像下面那样检索 client_secret.json 并尝试再次运行该脚本吗?


  1. 点击创建凭证作为OAuth客户端ID。

  2. 选中Other。


    • 借此,重定向网址会自动设定。该网址可用于快速入门。


  3. 输入姓名。

  4. 点击创建。

  5. 点击确定。

  6. 下载JSON。 > client_secret.json 。



注意:

在使用新的 client_secret.json 运行脚本之前,请删除现有的 calendar-nodejs-quickstart.json 。当使用默认的Quickstart时,该文件创建在home目录的 .credentials 中。



如果这不起作用对你而言,我很抱歉。

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

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);

Error and console output (values of JSON changed for security):

    λ 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)

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 client_secret.json as following and try to run the script, again?

  1. Click "Create credentials" as OAuth client ID.
  2. Check "Other".
    • By this, the redirect url is automatically set. The url can be used for Quickstart.
  3. Input Name.
  4. Click "Create".
  5. Click "OK".
  6. Download JSON.
  7. Rename JSON file to client_secret.json.

Note :

Before you run the script using new 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.

If this was not useful for you, I'm sorry.

这篇关于Google OAuth - 无法在授权时读取未定义的属性'0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆