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
divya manohardivya manohar 

response not in json format when using OAuth 2.0

Hi, 
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();
    
    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;
 }
}

visual force page : RestApiResponse
<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>

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

Thanks
divya