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
AnjaneyluAnjaneylu 

Unable to generate Security Token from REST API salesforce to salesforce Integration

Hi here i have created connected app in one org and in my another org  i have created this code to generate the security token. but here i am getting error so please help me and suggest me..
 
<apex:page controller="RestAPISFDC2SFDC_con">
  <apex:form >
  <apex:commandButton value="Get Access Token" action="{!getaccesstoken}"/> <BR/>
  status :: {!status}<BR/>
  statusCode:: {!statuscode}<BR/>
  Response: {!response}
  </apex:form>
</apex:page>

and Controller for the above page is 
 
public with sharing class RestAPISFDC2SFDC_con {
public string response{set;get;}
Public string status{set;get;}
Public integer statuscode{set;get;}
   public void getaccesstoken()
   {
       Http p=New Http();
       
       Httprequest req=new Httprequest();
       req.setendpoint('https://login.salesforce.com/services/oauth2/token');
       req.setmethod('POST');
      
      
    string Body='grant_type=password&client_id=3MVG9Y6d_Btp4xp5yX4.NCrDL2HiYHo_qJldFC.lJJOmdNJvPPdlbzlYK5LPISB9aWUbkBAx61Yk7q3L6Wexv&client_secret=5523536280322918040&username=aaa@xzz&password=password';  
    /* string Body='grant_type=password';
     Body=Body+'&client_id=3MVG9Y6d_Btp4xp5yX4.NCrDL2HiYHo_qJldFC.lJJOmdNJvPPdlbzlYK5LPISB9aWUbkBAx61Yk7q3L6Wexv';
     Body=Body+'&client_secret=5523536280322918040';
     Body=Body+'&username=anj@xyz.com';
     Body=Body+'&password=passwordMd8joDw2g6XFre8QurSBkxaS';*/
     
     req.setbody(body);
    // req.setheader('Content-Type','application/json');
     
     Httpresponse res=new Httpresponse();
     res=p.send(req);
     
     response=res.getbody();
     status=res.getstatus();
     statuscode=res.getstatuscode();
  }
}

Thanks in  Advance 
Anji Reddy
AnjaneyluAnjaneylu
here the error i am getting is 

status :: Bad Request
statusCode:: 400
Response: {"error":"invalid_grant","error_description":"authentication failure"}


 
Deepak GulianDeepak Gulian
Try this
string Body='grant_type=password&client_id=3MVG9Y6d_Btp4xp6i7amMyRkpuMUTRLqeqNYTr036XK7YoDYXlaWbZgJ_kLWXSJJQvJX50olzBSiy6jZOh.8r&client_secret=1595478626856505123&username=username&password=passwordsecuritytoken';

Replace Client_Id, Client_Secret, Username, Password & Security Token with your own values.
Temoc MunozTemoc Munoz
What I usually do is test my service with the REST Service Extension for Chrome:
https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US

Also, make sure both your password and API security token are included as Deepak described above.