GET request from APEX APIs 1.1 receiving 32/401


#1

Hi all,
I hope you can help me about this issue.

I’m trying to get followers Ids list (just to mention one) from Twitter using APEX code (salesforce).

I was using in the past Oauth APIs and those were working fine.

After I started approaching to the 1.1 APIs every kind of request is receiveing the 32/401 response, it should mean that I’m sending out a wrong Oauth header content.

So I decided to make a test:

  1. I used https://dev.twitter.com/rest/tools/console in order to generate a good request in terms of Oauth header details, so starting from the consol everything goes fine;

  2. I then used the same Oauth header (copy/paste) making a try from CURL (windows shell) and it is going fine as well, so also in this case I’m receiving the result with the full list of Followers/ids,

  3. using the same oauth header (authorization) is not working from APEX.

I have been searching for a while googling this issue but the answer is still not positive, just to add a bit see the code below:

**//create http instance**
        **Http http = new Http();**
            
        **//create request instance**
        **HttpRequest req = new HttpRequest();**
           
        **//set request details**
        **req.setMethod('GET');**
        
        **req.setEndpoint('https://api.twitter.com/1.1/followers/ids.json?cursor=-1&screen_name=myscreenname&user_id=myuserid');**
        
        **req.setBody('');**
        
        **//prepare the header**
                 
        **req.setHeader('X-Target-URI', 'https://api.twitter.com');**
        **req.setHeader('Authorization', 'OAuth oauth_consumer_key="myconsumerkey",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1431356060",oauth_nonce="randomnonce",oauth_version="1.0",oauth_token="mytoken",oauth_signature="mysignature"');**
        **req.setHeader('Host', 'api.twitter.com');**
        **req.setHeader('Connection', 'Keep-Alive');**
        
	
        **HTTPResponse res = http.send(req);**
            
       
                         
        **String resBody = res.getBody();**
        **System.debug('getBody: ' + resBody);**

Is there anyone that can help me with this ?

Best regards


#2

using the same oauth header

That is wrong. You can’t just use the same OAuth header, those need to be generated on each request as they contain encoded information about the current time and a one time nonce which will only be valid for short time. You should use a proper OAuth library and your problems should disappear.