代码之家  ›  专栏  ›  技术社区  ›  NealWalters

Google OAuth-无法在授权时读取未定义的属性“0”

  •  1
  • NealWalters  · 技术社区  · 7 年前

    我正在尝试使用谷歌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的值):

        λ 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\u secret.JSON”)。那么为什么重定向URL丢失了呢?也许这是我运行该设置时遗漏的内容?此外,这些指示意味着我正在为自己的日历设置一个API,在这里我想做一个API来读取其他人的日历。他们将其显示在网页上,因此它是公开的。(见我发布的相关问题: Can I use Google Calendar API v3 to access someone else's public calendar without auth? )

    1 回复  |  直到 7 年前
        1
  •  3
  •   Tanaike    7 年前

    你能取回吗 client_secret.json 然后再次尝试运行脚本?

    1. 单击“创建凭据”作为OAuth客户端ID。
    2. 勾选“其他”。
      • 由此,重定向url自动设置。url可用于快速启动。
    3. 输入名称。
    4. 单击“创建”。
    5. 单击“确定”。
    6. 下载JSON。
    7. 将JSON文件重命名为 client\u机密。json .

    注:

    在使用新脚本运行脚本之前 client\u机密。json ,请删除现有 calendar-nodejs-quickstart.json . 使用默认快速启动时,将在中创建文件 .credentials homedirectory的。

    如果这对你没用,我很抱歉。