Google OAuth - Cannot read property '0' of undefined at authorize

人走茶凉 提交于 2020-01-06 06:19:18

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/47514590/google-oauth-cannot-read-property-0-of-undefined-at-authorize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!