问题
I followed the instructions in this great post, to setup account linking between my app's server and google actions.
In the auth process, I requested "https://www.googleapis.com/auth/calendar" scope permission.
I managed to get the auth token on my server by calling
app.getUser().accessToken
But when I make a request to googleapi calendar, using this piece of code:
const google = require('googleapis');
var calendar = google.calendar('v3');
var eventData = {
auth: myAuthToken,
calendarId: 'primary',
resource: {
'summary': 'My Event',
'description': 'Event desc',
'start': {
'dateTime': '2017-06-11',
},
'transparency': 'transparent',
'visibility': 'private',
'colorId': 'blue'
}
};
calendar.events.insert(eventData,
function(err, event) {
if (err) {
console.log(err)
}
});
I get is this error:
{ Error: Login Required
at Request._callback (\node_modules\google-auth-library\lib\transporters.js:85:15)
at Request.self.callback (\node_modules\request\request.js:188:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (\node_modules\request\request.js:1171:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (\node_modules\request\request.js:1091:12)
at IncomingMessage.g (events.js:291:16)
at emitNone (events.js:91:20)
code: 401,
errors:
[ { domain: 'global',
reason: 'required',
message: 'Login Required',
locationType: 'header',
location: 'Authorization' } ] }
Is there any additional authentication steps I should follow?
回答1:
I think the issue is that the structure of what you're using as the auth
parameter isn't correct. You're passing it a string token, while it should be an OAuth2 object. See https://github.com/google/google-api-nodejs-client#making-authenticated-requests for details, but in short you will need to:
- Create an OAuth2 object
var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
- Set the credentials (the access token).
oauth2Client.setCredentials({
access_token: 'ACCESS TOKEN HERE'
});
- Use this
oauth2Client
object in theauth
parameter in your event data / call.
var eventData = {
auth: oauth2Client,
...
};
来源:https://stackoverflow.com/questions/44589700/google-actions-access-to-calendar-api-with-access-token-fails