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
ashish raiashish rai 

How to make httprequest with access token with example

Hi all,     

        Please any one send me the exact code for httprequest with accesstoken to retrive one org date from anothe org. I am sing this but geting respone as Invalid Token (but i ma puting the correct access token). So tell me what i am doing wrong in below code:

 

httprequest hreq=new httprequest();//https://login.salesforce.com/id/00D90000000aSjoEAE/00590000000eb7cAAAhreq.Setendpoint('https://ap1.salesforce.com');hreq.setmethod('GET');hreq.setHeader('Authorization: OAuth ', '00D90000000aSjo!AQgAQNfbCS3aunHX37ko2VhwXzk4holpYFrU9juuCIMdanXC54DAjI916nYNvzZHjOV2vRubCiT1XZGeongMBAVxtdMfpLlN');//hreq.setHeader('Content-Type', 'text/xml;charset=UTF-8');hreq.setHeader('SOAPAction','');hreq.setHeader('Host','https://ap1.salesforce.com');http htp=new http();httpResponse res=htp.send(hreq);system.debug('%%%%%%%%%%%%%' +res);System.debug('@@@@@@@@' +res.getBody());

 Please help me on this.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Ispita_NavatarIspita_Navatar

Hi Ashish,

Please find enclosed the exact code as desired by you:-

 

http h = new http();
 
        httprequest req = new httprequest();
        string session='00D90000000aSjo!AQgAQCgHhjzlPYTX0HPYCMqgl.INC5oocp7VyX4WnD4r3vJDBF5R3N.F.3EXr93AcW33qWtZ_e5sMm.bbxq6X0QNy0IsRwrz';
  //note that 'na1' may be different for your organization
       // req.setEndpoint('https://na1.salesforce.com/services/data/v20.0/');
       req.setEndpoint('https://ap1.salesforce.com/services/data/v23.0/chatter/feeds/news/me/feed-items');
        req.setMethod('GET');
        req.setHeader('Accept', 'application/xml;charset=UTF-8'); // When you use setMethod as 'GET' To get response in xml format.
  //req.setHeader('Content-Type', 'text/xml;charset=UTF-8'); // When you use setMethod as 'POST' To get response in xml format.
  req.setHeader('Authorization', 'OAuth ' + session);
        req.setTimeout(60000);
 
        httpresponse res = h.send(req);
 
        string result = res.getBody();
        system.debug('@@@@@@@@@@@@@@@@@@' +result);

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Ispita_NavatarIspita_Navatar

Hi Ashish,

Please find enclosed the exact code as desired by you:-

 

http h = new http();
 
        httprequest req = new httprequest();
        string session='00D90000000aSjo!AQgAQCgHhjzlPYTX0HPYCMqgl.INC5oocp7VyX4WnD4r3vJDBF5R3N.F.3EXr93AcW33qWtZ_e5sMm.bbxq6X0QNy0IsRwrz';
  //note that 'na1' may be different for your organization
       // req.setEndpoint('https://na1.salesforce.com/services/data/v20.0/');
       req.setEndpoint('https://ap1.salesforce.com/services/data/v23.0/chatter/feeds/news/me/feed-items');
        req.setMethod('GET');
        req.setHeader('Accept', 'application/xml;charset=UTF-8'); // When you use setMethod as 'GET' To get response in xml format.
  //req.setHeader('Content-Type', 'text/xml;charset=UTF-8'); // When you use setMethod as 'POST' To get response in xml format.
  req.setHeader('Authorization', 'OAuth ' + session);
        req.setTimeout(60000);
 
        httpresponse res = h.send(req);
 
        string result = res.getBody();
        system.debug('@@@@@@@@@@@@@@@@@@' +result);

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
rancpinerancpine

hi,

   Did this problem solved? I'm suffering the same issue. I got access token via password stream, but when I add it with "Authorization:OAuth XXXXX" into http header and post a request to the instance_url, server returns: "INVALID TOKEN"(1073)

 

  So what happend? what shall I do next?

rancpinerancpine

my problem is solved ;)

Thanks to the hint of endpoint. I checked my program again and found my endpoint path only contains instance_url. When I construct the path rightly, the packet works ok.

It's very confused when the endpoint is wrong but server return invalid token, though.