You need to sign in to do that
Don't have an account?
Sheeba Narayanan 7
How to get access token from google drive api using apex code
Im using below code to get access token from Google Drive and getting error says 'Invalid grant'. Here im passing code as Auth. Provider ID. Can anyone please assist on this?
try{
//Getting access token from google
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://accounts.google.com/o/oauth2/token');
req.setHeader('content-type', 'application/x-www-form-urlencoded');
String messageBody = 'code=' + code + '&client_id=' + key + '&client_secret=' + secret + '&redirect_uri=' + redirect_uri + '&grant_type=authorization_code';
req.setHeader('Content-length', String.valueOf(messageBody.length()));
req.setBody(messageBody);
req.setTimeout(60 * 1000);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug('Response :::' + resp);
JSONParser parser = JSON.createParser(resp);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
String fieldName = parser.getText();
parser.nextToken();
if (fieldName == 'access_token') {
accesstoken = parser.getText();
} else if (fieldName == 'expires_in') {
expiresIn = parser.getIntegerValue();
} else if (fieldname == 'token_type') {
tokentype = parser.getText();
}
} }
} catch
(Exception e) {
System.debug(LoggingLevel.ERROR,'Call exception ' + e.getMessage());
}
System.debug('access token ::::' + accesstoken);
try{
//Getting access token from google
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint('https://accounts.google.com/o/oauth2/token');
req.setHeader('content-type', 'application/x-www-form-urlencoded');
String messageBody = 'code=' + code + '&client_id=' + key + '&client_secret=' + secret + '&redirect_uri=' + redirect_uri + '&grant_type=authorization_code';
req.setHeader('Content-length', String.valueOf(messageBody.length()));
req.setBody(messageBody);
req.setTimeout(60 * 1000);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug('Response :::' + resp);
JSONParser parser = JSON.createParser(resp);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)) {
String fieldName = parser.getText();
parser.nextToken();
if (fieldName == 'access_token') {
accesstoken = parser.getText();
} else if (fieldName == 'expires_in') {
expiresIn = parser.getIntegerValue();
} else if (fieldname == 'token_type') {
tokentype = parser.getText();
}
} }
} catch
(Exception e) {
System.debug(LoggingLevel.ERROR,'Call exception ' + e.getMessage());
}
System.debug('access token ::::' + accesstoken);
Update as '&grant_type=\'authorization_code\'';
if authorization_code is a string, then '&grant_type='+'authorization_code';
Few references:
https://github.com/financialforcedev/ffhttp-googledrive#:~:text=Enter%20a%20Connector%20Type%20Name,the%20client%20ID%20obtained%20earlier.
https://help.salesforce.com/articleView?id=admin_files_connect_google_auth.htm&type=5
Hope above information was helpful.
Please mark as Best Answer so that it can help others in the future.
Thanks,
Vinay Kumar
It is not working.
What we need to pass for 'code'?Is that Auth. Provider ID?
Thanks
Sheeba
Review below references that can give you more details.
https://developer.salesforce.com/docs/atlas.en-us.api_streaming.meta/api_streaming/code_sample_auth_oauth.htm
https://trailhead.salesforce.com/content/learn/projects/build-a-connected-app-for-api-integration/implement-the-oauth-20-web-server-authentication-flow
Thanks,