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
subhajit13subhajit13 

Error in REST API call after getting the token

Hi, I am trying to make root call (or any other call like query as well), I am getting the below error:

 

System.HttpResponse[Status=Bad Request, StatusCode=400]

 Below is the code snippet, please let me know what am I missing here. 

 

Http http = new Http();
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();

req.setEndpoint('https://login.salesforce.com/services/data/v24.0');
req.setMethod('GET');
System.debug('Token in root() - '+token); //Getting this value from "https://login.salesforce.com/services/oauth2/token" call
String authVal = 'OAuth '+token;
req.setHeader('Authorization',authVal);
// Invoke web service call
String result = '';
try {
res = http.send(req);
result = res.getBody();
} catch(System.CalloutException e) {
 System.debug(res.toString());
}

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

the line that says setEndoint("https://login.saleforce.com/....") can't use login.salesforce.com, it needs to be na1.salesforce.com, or whichever instance your org is on (this is part of the token response if you want to discover it dynamically)

All Answers

SuperfellSuperfell

Only authentication services are available at login.salesforce.com, if you want to access the regular rest api then you need to use the correct instanceUrl/hostname for the user you're using.

subhajit13subhajit13

Thanks Simon, can you be litle more elaborate if possible with what you want me to change to make it work?

 

What I am trying currently is - 

 

In my developer Org, I am trying to run the basic REST calls (from same Org APEX class/VF) like generating token, making root call, query calls etc.

I am successfull in generating token but rest of the calls are not working currently with the mentioned error.

 

I worked with an pre-release org with same code sometime back, which used to return me correct result.

 

Thanks you for your help.

 

Regards,

Subhajit

SuperfellSuperfell

the line that says setEndoint("https://login.saleforce.com/....") can't use login.salesforce.com, it needs to be na1.salesforce.com, or whichever instance your org is on (this is part of the token response if you want to discover it dynamically)

This was selected as the best answer
subhajit13subhajit13

Thanks Simon,it works fine now, thanks for your input.

 

Regards,

Subhajit