+ Start a Discussion
divya manohardivya manohar 

response not in json format when using OAuth 2.0

I am trying to understand the basics of OAUTH 2.0 using username & password format.
I have 2 instances of sandbox api say user1 and user2 and both have system admin profile.
I have developed a rest api in user 1(say source) and I want to access the same in user2(say destination) by provding client ID,client secret,username and password.

Here is what I have done so far.

a) In user1(source) I have a rest api

b) In user2(destination), I have created connected app, enabled OAuth, provided the callback url as "https://ap2.salesforce.com/apex/RestApiResponse".

RestApiResponse is a visualforce page created in user2(destination)

c) In user2 in remote site settings, I have registered the url   https://login.salesforce.com/services/oauth/token

d) In user2 I have created apex controller and visual force page, the code is shown below
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();
    String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
    Http p=new Http();
    HttpResponse response=p.send(request);
    system.debug('@@@result = '+result);
    return null;

visual force page : RestApiResponse
<apex:page controller="restExample1">
  <apex:pageBlock title="RestApiExample">
       <apex:pageBlockSection title="OAuth2.0 username & Password"
       <apex:pageBlockSectionItem >
            <apex:outputLabel value="ClientID"/>
            <apex:inputText value="{!ClientID}"/>
       <apex:pageBlockSectionItem >
            <apex:outputLabel value="ClientSecret"/>
            <apex:inputText value="{!ClientSecret}"/>
       <apex:pageBlockSectionItem >
            <apex:outputLabel value="UserName"/>
            <apex:inputText value="{!UserName}"/>
       <apex:pageBlockSectionItem >
            <apex:outputLabel value="Password"/>
            <apex:inputText value="{!Password}"/>

       <apex:pageBlockButtons >
         <apex:commandButton value="Get Access Token"
  <apex:pageBlock title="Access Token"




when the vf pages executes and I click the command button, I see that the debug logs contains some HTML format and not the much required JSON string. , so I am unable to generate the accesstoken.

I am stuck here and please correct me if I am missing any step