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...
Here's how I got it to work so far.
1) Created a file called calendar-nodejs-quickstart.json in the root directory. I kept getting errors when trying to read .credentials/calendar-nodejs-quickstart.json. I tried setting the environment variables, but ended up changing sample code as follows:
var TOKEN_DIR = '.credentials/';
var TOKEN_PATH = 'calendar-nodejs-quickstart.json';
2) Had to remove "credentials :" from the beginning of the file, and add the double quotes (and also changed single quotes to double quotes). This was to get past various JSON parsing errors. { "access_token": "xxxxx", "refresh_token": "xxxx", "token_type": "Bearer", "expiry_date": 1512151860704 }
3) I also added the 'utf8' below, and added some debug code to see what was going on:
// Check if we have previously stored a token.
console.log("TOKEN_PATH=" + TOKEN_PATH);
fs.readFile(TOKEN_PATH, 'utf8', function(err, token) {
if (err) {
console.log("err=" + err);
getNewToken(oauth2Client, callback);
} else {
console.log("Use stored tokens from " + TOKEN_PATH);
console.log(token);
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
Seems critical to me to show the value of the "err" variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.