0

I am getting Invalid session id when I am executing the below code. What am I doing wrong? How can I fix it?

public class ResttestController {

    public String response{get;set;}
    public String dccName{get;set;}

    public PageReference Donate() {
        //find access token using Auth 2.0 
        String Access_Token='##############################################.$$$$$$$$$$$$$$$$$$$$$$$$$$$$$';
        Httprequest req=new httprequest();
        String domainName='ap2.salesforce.com';
        String endPointURL='https://'+domainName+'/services/data/v34.0/sobjects/Donations';
        req.setendpoint(endPointURL);
        req.setHeader('Content-Type', 'application/xml; charset=utf-8');
        req.setBody('<?xml version="1.0" encoding="UTF-8" ?><request><name>'+dccName+ </request>');
        req.setmethod('POST');
        req.setHeader('Authorization','Authorization: Bearer '+Access_Token);
        Http http = new Http();
        HTTPResponse res = http.send(req);
        response=res.getbody();
        return null;
    }
}
1

Change following line

req.setHeader('Authorization','Authorization: Bearer '+Access_Token);

to

req.setHeader('Authorization','Bearer '+Access_Token);

I assume that Access_Token is correct.

  • Hi.. Access_Token is the Consumer Key in connected apps.. Right???? i hv used the same.. I am still getting the same error – hellraizer Jul 20 '15 at 17:19
  • @hellraizer You have to request the access token first using ConsumerKey and ConsumerSecret. – RSG Jun 20 '17 at 10:25
1

To get the Access Token you need to run the Login API

How to Get Access Token

Once you get the access token you can use any of the salesforce REST API. You are passing the header wrong it should be like

req.setHeader('Authorization','Bearer ' + Access_Token);

then it should run properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.