function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sheeba Narayanan 7Sheeba 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);
VinayVinay (Salesforce Developers) 
Hi Sheeba,

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
Sheeba Narayanan 7Sheeba Narayanan 7
Thanks Vinay
It is not working.
What we need to pass for 'code'?Is that Auth. Provider ID?

Thanks
Sheeba
 
VinayVinay (Salesforce Developers) 
To initiate the OAuth 2.0 web server flow, the external web service—via the connected app—posts an authorization code request using the authorization code grant type to the Salesforce authorization endpoint
 
curl https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=myclientid" -d "client_secret=myclientsecret" 
    -d "username=mylogin@salesforce.com" -d "password=mypassword123456"

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,