4

I've been trying to read through the SalesForce documentation on how to call a custom Method like this via cURL:

@RestResource(urlMapping='/Test/*')
global with sharing class RestTest 
{
    @HttpGet
    global static Account getAccount()
    {
        Account a = [select Id, Name from Account WHERE Name = 'wizzbang' limit 1];
        return a;
    }
}

My gut tells me this should be something as simple as:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" 
-X GET https://mydomain.salesforce.com/services/apexrest/test/

But I get back that this is unauthenticated. I've gone through the guide and it says I have to setup a "connected app." I've done this...I think. But I'm not sure what my CallbackURL should be? Further, I don't get what I need to make my curl statement look like in order to call the service? Can someone explain what I need to do? This seems horribly overengineered.

  • Cool to know, but I'd really like to know how this is done with authentication. It's something I'd like, but how it's actually done with something like curl is pretty poorly documented IMO. – Darkenor Jun 18 '14 at 21:50
  • The callback URL for oAuth2 is: login.salesforce.com/services/oauth2/token for production and test.salesforce.com/services/oauth2/token for sandbox. – cropredy Jun 18 '14 at 22:18
  • Moved this back to be a comment... (Assuming your org does not contain real data) if you want to sidestep authentication for now, you can create a Force.com Site and add the class to it: sites allow unauthenticated access. A Force.com Site only takes a few minutes to setup - see e.g. An Introduction to Force.com Sites. You must add the REST class to the Site's profile which is accessible by clicking the "Public Access Settings" button on the Site detail page. – Keith C Jun 18 '14 at 22:26
6

You can create a connected app with a dummy endpoint (http://localhost would work just fine) then do OAuth username/password authentication in curl:

$ curl -H 'X-PrettyPrint: 1' \
    -d 'grant_type=password&client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>&username=<USERNAME>&password=<PASSWORD>' \
    https://login.salesforce.com/services/oauth2/token

Response:

{
  "id" : "https://login.salesforce.com/id/<ORD_ID>/<USER_ID>",
  "issued_at" : "<ISSUE_TIME>",
  "token_type" : "Bearer",
  "instance_url" : "https://<INSTANCE>.salesforce.com",
  "signature" : "<SIGNATURE>",
  "access_token" : "<ACCESS_TOKEN>"
}

Now you can call the REST method with the access token:

curl -H 'X-PrettyPrint: 1' \
    -H 'Authorization: Bearer <ACCESS_TOKEN>' \
    https://<INSTANCE>.salesforce.com/services/apexrest/<PATH>

And you should get a response like this (I used sForce as the account name):

{
  "attributes" : {
    "type" : "Account",
    "url" : "/services/data/v30.0/sobjects/Account/001E0000002Jv2mIAC"
  },
  "Name" : "sForce",
  "Id" : "001E0000002Jv2mIAC"
}
  • They sure don't make THAT easy/clear, do they? :) Thank you! – Darkenor Jun 19 '14 at 12:39

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.