Reputation: 1433
I have a mobile application that gains access to user's calendar (Microsoft). Once I get the access token, I save it in my backend database for reuse. I then - separately - call the Microsoft Graph API from node.js API with the saved token which works fine :
var microsoftGraph = require("@microsoft/microsoft-graph-client");
var client = microsoftGraph.Client.init({
authProvider: (done) => {
done(null, token);
}
});
client.api('/me/calendar/events/xyz').patch({
'Body': {
'ContentType': '0',
'Content': 'test from api 2'
}
}, (err, res) => {
if (err) {
console.log('err: ', err);
} else {
console.log('res: ', res);
}
});
The problem is the token expires and it doesn't come with refresh token to renew it from server without user intervention.
Here is the error I get after ~2 hours:
err: {
statusCode: 401,
code: 'InvalidAuthenticationToken',
message: 'CompactToken validation failed with reason code: -2147184088.',
requestId: '5bdd2402-b1b9-4c25-8b2d-1ca2c4a79192',
date: 2017 - 08 - 07 T19: 27: 44.000 Z,
body: {
code: 'InvalidAuthenticationToken',
message: 'CompactToken validation failed with reason code: -2147184088.',
innerError: {
'request-id': '5bdd2402-b1b9-4c25-8b2d-1ca2c4a79192',
date: '2017-08-07T15:27:44'
}
}
}
What am I missing here? There has to be a way to integrate a server side code with Microsoft Graph without the need to renew tokens from client side?
Upvotes: 2
Views: 568
Reputation: 2447
Access tokens expire after 60 minutes, so you'll either need to get a new one or use a refresh token. Refresh tokens can be used to get a new access token and the docs for that API call are here: https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_user#5-use-the-refresh-token-to-get-a-new-access-token
To get a refresh token, request the offline_access
permission and the user will see this in the consent screen as "Access your data anytime". An example of requesting the offline_access
scope can be found here: https://developer.microsoft.com/en-us/graph/docs/concepts/auth_v2_user#2-get-authorization
Upvotes: 4