[SalesForce] Having problems with authenticating to the custom REST class

I keep getting 401 Not Authorized errors when I call my resource with a REST test tool like the Advanced REST client. I'm supplying an access token taken from a curl call to the salesforce login service. The error I get back when using the RestResource is

  message: "Session expired or invalid"
errorCode: "INVALID_SESSION_ID"

My annotation for the RestResource is

@RestResource( urlMapping='/CreateObject/v1/*')

And I'm calling my service via the URL https://emea.salesforce.com/services/apexrest/CreateObject/v1/*

Can anyone shed a light as to what I'm doing wrong?

Best Answer

You should also include the session ID in your rest call

for eg if you're calling from jQuery Ajax include the authorization header like below:

$.ajax({
url : "https://instance/services/data/v26.0/sobjects/global/describe/layouts/",
headers : {"Authorization": "Bearer AddyoursessionIdhere"},
contentType : "application/json",
type :"GET"
}).done(function(response){
    console.log('the result '+ JSON.stringify(response));

}).error(function(error){
    console.log('the error mesasge is '+ JSON.stringify(error));
});

or in curl, include the below header along with the request

-H "Authorization: Bearer sessionId

You can go thru (authentication section) this on how to get the session Id in curl.. http://www.salesforce.com/us/developer/docs/api_rest/

But if you want to make the REST public then you can look it up here: http://www.wadewegner.com/2013/03/creating-anonymous-rest-apis-with-salesforce-com/

Also instead of using the REST client you can use this link http://www.hurl.it to make simple GET requests

Related Topic