1

So I have set up a Named Credentials with information about my Twitter App. I know the authentication is working correctly becacause my named credential looks like this: enter image description here

And it says I am authenticated as my twitter account.

Here is my code to make a twitter api request:

public static void sendHttpPostRequest(string url)
{
    Http h = new Http();
    HttpResponse res = new HttpResponse();
    HttpRequest req = new HttpRequest();
    req.setEndpoint(url);
    req.setMethod('POST');
    req.setTimeout(2000); //2 seconds
    req.setBody('');
    try
    {
        res = h.send(req);
        system.debug('HTTP-DEBUG: Sent POST request successfully');
        system.debug(('reponse is ' + res.getBody()));
    }
    catch(exception e)
    {
        system.debug('HTTP-DEBUG: Failed to send post request due to ' + e);
        system.debug(res.toString());
    }
}

When I try and post a status to my twitter account using:

HttpUtility.sendHttpPostRequest('callout:TwitterAPI/statuses/update.json?status=this%20is%20a%20test');

I get the following response from twitter:

{"errors":[{"code":215,"message":"Bad Authentication data."}

Any help?

closed as off-topic by glls, battery.cord, Raul, Gortonington, Himanshu Oct 31 '17 at 16:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – glls, battery.cord, Raul, Gortonington, Himanshu
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Thanks for the link but I'm a little confused about what I'm supposed to get from it? It appears to be about API requests that are non authenticated - however salesforce should be authenticating my request – Hans Donkersloot Oct 30 '17 at 19:59
  • based on the status code, it doesnt look like it. try debugging your code and check what header you are sending – glls Oct 30 '17 at 20:05
  • When I debug my code(even after getting the response) I don't see any headers sent. I believe the headers are automatically set in the http.send method(along with converting the endpoint to the correct endpoint: 'api.twitter.com/TwitterAPI/statuses/…'. In the name credentials page it says: 'if selected, the named credential generates and authorization header and applies it to each callout that references the named credential'. And since The named credential is being invoked(since I get a response from twitter) the headers must be set – Hans Donkersloot Oct 30 '17 at 20:19