How to use the Google Contacts API in Meteor?
I am using meteor to create a web page with a drop down list of google groups to select and after selecting it, google contacts will be displayed.
I am using HTTP.call POST to google API and am testing with accessToken from mongoDB but when I use this token after a while it expires. I have looked into the implementation of the authentication flow, but it gets very complicated as there is no sample code for meteorite on google. I am new to nodeJS, Javascript and Meteor. Am I wrong? How to implement this in a meteor?
https://developers.google.com/accounts/docs/OAuth2?csw=1#expiration
source to share
To fix the accessToken expiration issue, you need to get the refreshToken from google. With this refreshToken, you can get a new accessToken when needed with a simple HTTP POST to the Google API. Here is the relevant documentation from Google. To get the refreshToken, you will need to request offline access, and a forced approval prompt may also be required as described in the SO post .
forceApprovalPrompt: {google: true},
requestOfflineToken: {google: true},
I recommend achieving all of the above using the Meteor HTTP package . All the tools are there. You probably already figured this out:
var result = HTTP.post(
"https://www.googleapis.com/oauth2/v3/token",
{
params: {
'client_id': config.clientId,
'client_secret': config.secret,
'refresh_token': user.services.google.refreshToken,
'grant_type': 'refresh_token'
}
});
//Do some error checking here
var newAccessToken = result.data.access_token;
- refresh_token - refresh token returned from authorization code exchange.
- client_id . Client ID obtained from Developer Console.
- client_secret - client secret obtained from the Developer Console.
- grant_type . As defined in the OAuth 2.0 specification, this field must contain the refresh_token value.
-
result.data will be a JSON object with the following
{"Access_token": "1 / fFBGRNJru1FQd44AzqT3Zg", "Expires_in": 3920, "Token_type": "Standard bearer",}
source to share