I'm using my own gmail user to read a public calendar. Got program working locally, and displayed the credentials/token with console.log (value altered to protect my token):
Got Token
OAuth2Client {
transporter: DefaultTransporter {},
_certificateCache: null,
_certificateExpiry: null,
_clientId: 'xxxxxxxxxxxxxx',
_clientSecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
_redirectUri: 'urn:ietf:wg:oauth:2.0:oob',
_opts: {},
credentials:
{ access_token: 'xxxxxxx',
refresh_token: 'xxxxxxxxxxxxxxxxxx',
token_type: 'Bearer',
expiry_date: 1512151860704 } }
I also did what StackOverflow said: How to oAuth Google API from Lambda AWS? and it gave me the same access_token as displayed above.
So, if I understand, I need to take the access token and put it in my program or a file, and I'm not sure how to do that. My code came from the Google example here: https://developers.google.com/google-apps/calendar/quickstart/nodejs
Do I put this token somewhere in my client_secret.json file or what? I tried just passing it straight to the listEvents method as the value of TOKEN but got "400 Bad Request".
Update 1: I tried storing the file to disk and then reading it as follows:
exports.getCalendarJSONEventsNew =
function getCalendarJSONEvents(callback) {
console.log("started getCalendarJSONEventsNew");
fs.readFile('OAuth2Client.json', 'utf8',
function processedFile(err, content) {
if (err) {
console.log('Error loading OAuth2Client.json: ' + err);
return;
}
console.log("content=");
console.log(content);
var tokenFromFile = JSON.parse(content);
listEvents(tokenFromFile, function(jsonResult) {
console.log("Json Callback Events=");
console.log(jsonResult);
callback(jsonResult);
});
});
}
Error: It doesn't seem to be exactly be JSON, so not how to deserialize it back into object:
OAuth2Client {
^
SyntaxError: Unexpected token O in JSON at position 0
Update 2: Then I had another idea, I saved the following as
credentials: {
access_token: 'xxxxx',
refresh_token: 'xxxxxx',
token_type: 'Bearer',
expiry_date: 1512151860704
}
as .credentials/calendar-nodejs-quickstart.json.
Then when I ran on the server, I got this response back:
Authorize this app by visiting this url: https://accounts.google.com/o/oauth2/auth?etc...