1

I am trying to generate the access token using OAUTH 2.0 . Here is the full code but repsonse.getBody() is not giving me the JSON string.

I have 2 instances of salesforce sandbox api, data flow is from salesforceA(source) to salesforceB(destination)

Both users are having system admin profile.

In salesforceA(source) I have created a connected app, so I get clientID and client secret, callback URL is: http://ap2.salesforce.com/apex/MyResponse where Myresponse is a visualforce page.

In salesforceB(destination), in remote site settings I have registered the URL "https://login.salesforce.com/services/oauth/token'

apex controller and visual force page is in salesforceB(destination)

public class restExample1
{
  public String ClientSecret{get;set;}
  public String ClientID{get;set;}
  public String UserName{get;set;}
  public String Password{get;set;}
  public String Result{get;set;}
  public String accessToken{get;set;}


  public PageReference GetAccessToken()
  {
HttpRequest request=new HttpRequest();

request.setEndPoint('https://login.salesforce.com/services/oauth/token');

request.setMethod('POST');

String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;

request.setBody(reqbody);

Http p=new Http();

HttpResponse response=p.send(request);

result=response.getBody();

system.debug('@@@result = '+result);

return null;
}
}

Myresponse visualforce page

<apex:page controller="restExample1">
<apex:form >
  <apex:pageBlock title="RestApiExample">

       <apex:pageBlockSection title="OAuth2.0 username & Password"
                              collapsible="false">


       <apex:pageBlockSectionItem >
            <apex:outputLabel value="ClientID"/>

            <apex:inputText value="{!ClientID}"/>

       </apex:pageBlockSectionItem>

       <apex:pageBlockSectionItem >
            <apex:outputLabel value="ClientSecret"/>

            <apex:inputText value="{!ClientSecret}"/>

       </apex:pageBlockSectionItem>


       <apex:pageBlockSectionItem >
            <apex:outputLabel value="UserName"/>

            <apex:inputText value="{!UserName}"/>

       </apex:pageBlockSectionItem>

       <apex:pageBlockSectionItem >
            <apex:outputLabel value="Password"/>

            <apex:inputText value="{!Password}"/>

       </apex:pageBlockSectionItem>


       </apex:pageBlockSection>

       <apex:pageBlockButtons >

         <apex:commandButton value="Get Access Token"
                             action="{!GetAccessToken}"
                             reRender="token"/>

       </apex:pageBlockButtons>
  </apex:pageBlock>

  <apex:pageBlock title="Access Token"
                  id="token">


     {!accesstoken}

   </apex:pageBlock>

</apex:form>

</apex:page>
  • What is repsonse.getBody() giving you ? – superfell Jun 16 '16 at 4:07
  • for sandboxes, I've used setEndPoint('https://test.salesforce.com/services/oauth2/token'); – cropredy Jun 16 '16 at 4:08
  • response.getBody() is giving me HTML code. – Pooja Biswas Jun 16 '16 at 4:14
  • if I use endpoint as 'test.salesforce.com/services/oauth2/token', then I in debug logs I getting authenticaltion faliure "result = {"error":"invalid_grant","error_description":"authentication failure"" – Pooja Biswas Jun 16 '16 at 4:18
0

I checked your code it is working fine. I think the issue is related with security token. You have to pass "Password+Security token" in password field to get the Access Token.

  • I am getting the same error: authentication failure, after giving password+securitytoken of source. Can u pls tell did u check with password+security token – Pooja Biswas Jun 16 '16 at 7:16
  • Yes, I checked with password+security. Are you using the correct endpoint https://test.salesforce.com/services/oauth2/token?. Also verify your username, password+security token combination in Datalaoder. – Saroj Bera Jun 16 '16 at 7:21
  • I hv used same endpoint url as u mentioned and checked the source credentials in dataloader its working. can u pls confirm if u hv followed the same steps I hv given becas I am perplexed ur getting json but I am not? – Pooja Biswas Jun 16 '16 at 7:43
  • I am using exactly same code as yours. – Saroj Bera Jun 16 '16 at 8:42
  • I am using google chrome and Microsoft edge browsers. Do I hv to install any browser extensions to make my requirement work?Also I have enabled connected apps settings in both the profiles. – Pooja Biswas Jun 19 '16 at 2:59
0

Please do below two corrections:

Set the content type on request

request.setHeader('Content-Type', 'application/x-www-form-urlencoded');

Correct the endpoint from https://login.salesforce.com/services/oauth/token to https://login.salesforce.com/services/oauth2/token

You have missed oauth2.

Also you need to encode the username and password.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.