You need to sign in to do that
Don't have an account?
Arav Sundar
Auth Error : Invalid Grant
VF page
<apex:page controller="OAuthDemoPageController">
<apex:form >
<apex:pageBlock title="OAuth Demo" mode="edit">
<apex:pageBlockSection title="Authorization Code" columns="1">
<apex:pageBlockSectionItem >
Your Authorization Code
<apex:outPutText >{!authorizationCode}</apex:outPutText>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Access Token">
<apex:commandButton value="Get Access Token" action="{!getAccessToken}"/>
<apex:pageBlockSectionItem >
Your Access Token
<apex:outPutText >{!accessToken}</apex:outPutText>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Upload File">
<apex:inputFile value="{!attachmentBody}" fileName="{!attachmentName}"></apex:inputFile>
<apex:commandButton value="UPLOAD FILE" action="{!uploadFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class
public class OAuthDemoPageController{
public String authorizationCode {get;set;}
public String accessToken {get;set;}
public transient blob attachmentBody {get;set;}
public string attachmentName {get;set;}
public OAuthDemoPageController(){
authorizationCode = ApexPages.CurrentPage().getParameters().get('code');
}
public void getAccessToken(){
String grantTypeString = 'grant_type=authorization_code';
String authorizationCodeString = '&code='+authorizationCode;
String clientIdString = '&client_id=191713897201-mg7kgkhpd1h8up6behjc9a635nj86bma.apps.googleusercontent.com';
String clientSecretString = '&client_secret=VfJRs-MBGgwKQ3bb-Zo8j_NH';
String redirectURIString = '&redirect_uri=https://c.ap2.visual.force.com/apex/OAuthDemoPage';
String requestBody = grantTypeString + authorizationCodeString + clientIdString + clientSecretString + redirectURIString;
HttpRequest req = new HttpRequest();
req.setEndpoint('https://www.googleapis.com/oauth2/v3/token');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setBody(requestBody );
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('RESPONSE BODY---> '+res.getBody());
GoogleAPIResponseWrapper googleResponse = (GoogleAPIResponseWrapper) JSON.deserialize(res.getBody(), GoogleAPIResponseWrapper.Class);
accessToken = googleResponse.access_token;
}
public void uploadFile(){
GoogleDriveFileUploadHandler.uploadFile(attachmentName, attachmentBody, accessToken);
}
}
Debug Logs
36.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
16:50:46.0 (662273)|EXECUTION_STARTED
16:50:46.0 (707459)|CODE_UNIT_STARTED|[EXTERNAL]|06628000004EHZv|VF: /apex/OAuthDemoPage
16:50:46.0 (1946813)|VF_DESERIALIZE_VIEWSTATE_BEGIN|06628000004EHZv
16:50:46.0 (21984877)|VF_DESERIALIZE_VIEWSTATE_END
16:50:46.0 (25779298)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController invoke(getAccessToken)
16:50:46.0 (26796425)|CALLOUT_REQUEST|[28]|System.HttpRequest[Endpoint=https://www.googleapis.com/oauth2/v3/token, Method=POST]
16:50:46.0 (844669215)|CALLOUT_RESPONSE|[28]|System.HttpResponse[Status=Bad Request, StatusCode=400]
16:50:46.0 (844920747)|USER_DEBUG|[29]|DEBUG|RESPONSE BODY---> {
"error": "invalid_grant",
"error_description": "Bad Request"
}
16:50:46.0 (851454005)|CODE_UNIT_FINISHED|OAuthDemoPageController invoke(getAccessToken)
16:50:46.0 (852131918)|VF_APEX_CALL|j_id9|{!getAccessToken}|PageReference: none
16:50:46.0 (880204589)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController get(authorizationCode)
16:50:46.0 (880222476)|SYSTEM_MODE_ENTER|true
16:50:46.0 (880239984)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|authorizationCode
16:50:46.0 (880260100)|CODE_UNIT_FINISHED|authorizationCode
16:50:46.0 (880286428)|CODE_UNIT_FINISHED|OAuthDemoPageController get(authorizationCode)
16:50:46.0 (880730009)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController get(accessToken)
16:50:46.0 (880737624)|SYSTEM_MODE_ENTER|true
16:50:46.0 (880746006)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|accessToken
16:50:46.0 (880754720)|CODE_UNIT_FINISHED|accessToken
16:50:46.0 (880767385)|CODE_UNIT_FINISHED|OAuthDemoPageController get(accessToken)
16:50:46.0 (883135783)|VF_SERIALIZE_VIEWSTATE_BEGIN|06628000004EHZv
16:50:46.0 (884272275)|VF_SERIALIZE_VIEWSTATE_END
16:50:46.887 (887216183)|CUMULATIVE_LIMIT_USAGE
16:50:46.887 (887216183)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 1 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10
16:50:46.887 (887216183)|CUMULATIVE_LIMIT_USAGE_END
16:50:46.0 (887272335)|CODE_UNIT_FINISHED|VF: /apex/OAuthDemoPage
16:50:46.0 (888406109)|EXECUTION_FINISHED
I was trying with the google have got the customer id and service key by creating an project in the google console , can someone help me with the suggestion
<apex:page controller="OAuthDemoPageController">
<apex:form >
<apex:pageBlock title="OAuth Demo" mode="edit">
<apex:pageBlockSection title="Authorization Code" columns="1">
<apex:pageBlockSectionItem >
Your Authorization Code
<apex:outPutText >{!authorizationCode}</apex:outPutText>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Access Token">
<apex:commandButton value="Get Access Token" action="{!getAccessToken}"/>
<apex:pageBlockSectionItem >
Your Access Token
<apex:outPutText >{!accessToken}</apex:outPutText>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockSection title="Upload File">
<apex:inputFile value="{!attachmentBody}" fileName="{!attachmentName}"></apex:inputFile>
<apex:commandButton value="UPLOAD FILE" action="{!uploadFile}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class
public class OAuthDemoPageController{
public String authorizationCode {get;set;}
public String accessToken {get;set;}
public transient blob attachmentBody {get;set;}
public string attachmentName {get;set;}
public OAuthDemoPageController(){
authorizationCode = ApexPages.CurrentPage().getParameters().get('code');
}
public void getAccessToken(){
String grantTypeString = 'grant_type=authorization_code';
String authorizationCodeString = '&code='+authorizationCode;
String clientIdString = '&client_id=191713897201-mg7kgkhpd1h8up6behjc9a635nj86bma.apps.googleusercontent.com';
String clientSecretString = '&client_secret=VfJRs-MBGgwKQ3bb-Zo8j_NH';
String redirectURIString = '&redirect_uri=https://c.ap2.visual.force.com/apex/OAuthDemoPage';
String requestBody = grantTypeString + authorizationCodeString + clientIdString + clientSecretString + redirectURIString;
HttpRequest req = new HttpRequest();
req.setEndpoint('https://www.googleapis.com/oauth2/v3/token');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setBody(requestBody );
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('RESPONSE BODY---> '+res.getBody());
GoogleAPIResponseWrapper googleResponse = (GoogleAPIResponseWrapper) JSON.deserialize(res.getBody(), GoogleAPIResponseWrapper.Class);
accessToken = googleResponse.access_token;
}
public void uploadFile(){
GoogleDriveFileUploadHandler.uploadFile(attachmentName, attachmentBody, accessToken);
}
}
Debug Logs
36.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
16:50:46.0 (662273)|EXECUTION_STARTED
16:50:46.0 (707459)|CODE_UNIT_STARTED|[EXTERNAL]|06628000004EHZv|VF: /apex/OAuthDemoPage
16:50:46.0 (1946813)|VF_DESERIALIZE_VIEWSTATE_BEGIN|06628000004EHZv
16:50:46.0 (21984877)|VF_DESERIALIZE_VIEWSTATE_END
16:50:46.0 (25779298)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController invoke(getAccessToken)
16:50:46.0 (26796425)|CALLOUT_REQUEST|[28]|System.HttpRequest[Endpoint=https://www.googleapis.com/oauth2/v3/token, Method=POST]
16:50:46.0 (844669215)|CALLOUT_RESPONSE|[28]|System.HttpResponse[Status=Bad Request, StatusCode=400]
16:50:46.0 (844920747)|USER_DEBUG|[29]|DEBUG|RESPONSE BODY---> {
"error": "invalid_grant",
"error_description": "Bad Request"
}
16:50:46.0 (851454005)|CODE_UNIT_FINISHED|OAuthDemoPageController invoke(getAccessToken)
16:50:46.0 (852131918)|VF_APEX_CALL|j_id9|{!getAccessToken}|PageReference: none
16:50:46.0 (880204589)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController get(authorizationCode)
16:50:46.0 (880222476)|SYSTEM_MODE_ENTER|true
16:50:46.0 (880239984)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|authorizationCode
16:50:46.0 (880260100)|CODE_UNIT_FINISHED|authorizationCode
16:50:46.0 (880286428)|CODE_UNIT_FINISHED|OAuthDemoPageController get(authorizationCode)
16:50:46.0 (880730009)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|OAuthDemoPageController get(accessToken)
16:50:46.0 (880737624)|SYSTEM_MODE_ENTER|true
16:50:46.0 (880746006)|CODE_UNIT_STARTED|[EXTERNAL]|01p28000007E1sX|accessToken
16:50:46.0 (880754720)|CODE_UNIT_FINISHED|accessToken
16:50:46.0 (880767385)|CODE_UNIT_FINISHED|OAuthDemoPageController get(accessToken)
16:50:46.0 (883135783)|VF_SERIALIZE_VIEWSTATE_BEGIN|06628000004EHZv
16:50:46.0 (884272275)|VF_SERIALIZE_VIEWSTATE_END
16:50:46.887 (887216183)|CUMULATIVE_LIMIT_USAGE
16:50:46.887 (887216183)|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 0 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 1 out of 100
Number of Email Invocations: 0 out of 10
Number of future calls: 0 out of 50
Number of queueable jobs added to the queue: 0 out of 50
Number of Mobile Apex push calls: 0 out of 10
16:50:46.887 (887216183)|CUMULATIVE_LIMIT_USAGE_END
16:50:46.0 (887272335)|CODE_UNIT_FINISHED|VF: /apex/OAuthDemoPage
16:50:46.0 (888406109)|EXECUTION_FINISHED
I was trying with the google have got the customer id and service key by creating an project in the google console , can someone help me with the suggestion