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
farukh sk hdfarukh sk hd 

HttpResponse[Status=Unauthorized, StatusCode=401]

Can somebody help me here I am getting the error while getting data based on id which I am passing, I am able to generate session id as well.

Salesforce to Salesforce Rest Api Integration.

My code is,

public class restApiClassSalesforceorg1{


private string cKey='XXXXXX';
private string cSecret='XXXXXX';
private string uName='XXXXXX';
private string passwd='XXXXXX';

// Above information is hided


public class responseWrapper{

public string id;
public string access_token;
public string instance_url;

}

public string getRequestToken()
{
string reqBody='grant_type=password&client_id='+cKey+'&client_secret='+cSecret+'&username='+uName+'&password='+passwd;                                      

Http h=new Http();
HttpRequest req=new HttpRequest();
req.setBody(reqBody);
req.setMethod('POST');
req.setEndpoint('https://login.salesforce.com/services/oauth2/token');

HttpResponse hresp=h.send(req);
responseWrapper wResp=(responseWrapper)JSON.deserialize(hresp.getBody(),responseWrapper.class);
system.debug('Instance url'+wResp.instance_url);
system.debug('session id'+wResp.access_token);
return wResp.access_token;


}

public static list<Account> getConList()
{
list<account> accList1=new list<account>();
String accToken;
string ExternalId='SomeIDfromOtherOrg';
string responseBody;
string endPoint='https://ap5.salesforce.com/services/apexrest/v1/getAccountOnExternalId/' + ExternalId;
restApiClassSalesforceorg1 obj=new restApiClassSalesforceorg1();
accToken=obj.getRequestToken();
system.debug('access token'+ accToken);
if(accToken!='')
{
Http h1=new Http();
HttpRequest req1=new HttpRequest();
req1.setHeader('Authorization','Bearer'+accToken);
req1.setHeader('Content-Type','application/json');
//req1.setHeader('Accept','application/json');
req1.setMethod('GET');
req1.setEndpoint(endPoint);
HttpResponse hresp1=h1.send(req1);
responseBody=hresp1.getBody();
JSONParser parser=JSON.createParser(responseBody);
 while (parser.nextToken() != null) {
 Account obj1=new Account();
 if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'Name')) {
 
 string accName=parser.getText();
 obj1.name=accName;
 
 }
accList1.add(obj1);
}
}
return accList1;

}



}

Code from other org,

@RestResource(urlMapping='/v1/getAccountOnExternalId/*')
   global with sharing class getAccount {
     @Httpget
      global static list<Account> fetchAccount(){
        RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        string accId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
        list<Account> lstAcc=[Select id , name,ExternalIdForAccount__c from Account where Id=:accId ];
        
        return lstAcc;
      }
   }


 
Best Answer chosen by farukh sk hd
v varaprasadv varaprasad
Hi Farukh,

req1.setHeader('Authorization','Bearer'+accToken);

Use below the line we need to give space between Bearer and token.
req1.setHeader('Authorization','Bearer '+accToken);
Please check once below video:

https://www.youtube.com/watch?v=7PaDtgPLH90&t=4s

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


Thanks,
V Varaprasad
Email: varaprasad4sfdc@gmail.com
 

All Answers

v varaprasadv varaprasad
Hi Farukh,

req1.setHeader('Authorization','Bearer'+accToken);

Use below the line we need to give space between Bearer and token.
req1.setHeader('Authorization','Bearer '+accToken);
Please check once below video:

https://www.youtube.com/watch?v=7PaDtgPLH90&t=4s

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


Thanks,
V Varaprasad
Email: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi farukh,

Please refer to these links it may be helpful to you.
https://developer.salesforce.com/forums/?id=906F0000000AWGOIA4
https://developer.salesforce.com/forums/?id=906F000000094PmIAI

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha