【问题标题】:Google OAuth - Cannot read property '0' of undefined at authorizeGoogle OAuth - 在授权时无法读取未定义的属性“0”
【发布时间】:2021-02-02 07:23:15
【问题描述】:

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

    λ 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 来读取别人的日历。他们将其显示在网页上,因此是公开的。 (见我贴的相关问题:Can I use Google Calendar API v3 to access someone else's public calendar without auth?

【问题讨论】:

    标签: node.js google-api google-calendar-api


    【解决方案1】:

    您能否按以下方式检索client_secret.json 并再次尝试运行该脚本?

    1. 单击“创建凭据”作为 OAuth 客户端 ID。
    2. 选中“其他”。
      • 这样,重定向 url 被自动设置。该网址可用于快速入门。
    3. 输入名称。
    4. 点击“创建”。
    5. 点击“确定”。
    6. 下载 JSON。
    7. 将 JSON 文件重命名为 client_secret.json

    注意:

    在使用新的client_secret.json 运行脚本之前,请删除现有的calendar-nodejs-quickstart.json。使用默认快速启动时,会在主目录的.credentials 中创建文件。

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

    【讨论】:

    • 抱歉,直到今天才看到回复。工作!我不必做“注意:”部分,找不到任何日历-nodejs-quickstart-.json。它给了我一个 URL,然后我不得不从该网页粘贴代码。我之前使用的是“网站”而不是“其他”,这会有所不同吗?
    • 它正在读取我的日历,现在我将尝试弄清楚如何将它指向另一个公共日历。
    • @NealWalters "Other" 在初始阶段有 "urn:ietf:wg:oauth:2.0:oob","http://localhost" 的 2 个重定向 uri。 Quickstart 可以使用它们。所以我认为用户很容易使用这种模式。在 webapp,用户必须设置重定向 uri。我认为这可能导致错误的原因。
    猜你喜欢
    • 2014-08-09
    • 1970-01-01
    • 2022-11-03
    • 2017-12-30
    • 2018-09-26
    • 1970-01-01
    • 2019-11-20
    相关资源
    最近更新 更多