问题
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?
- Click "Create credentials" as OAuth client ID.
- Check "Other".
- By this, the redirect url is automatically set. The url can be used for Quickstart.
- Input Name.
- Click "Create".
- Click "OK".
- Download JSON.
- 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