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
Duncan_StewartDuncan_Stewart 

Apex REST Basic Code sample does not describe how to retrieve the sessionId

I'm working on My First REST Service (soon to be released), leveraging all of the howTo's that I can find, starting with Apex REST Basic Code Sample.

I created the MyRestResource class using the example, brought up curl, and was stopped abruptly on this line:

  • Replace sessionId with the <sessionId> element that you noted in the login response.

There isn't anything preceding this line ^ that describes how to get that initial login response that contains the session Id.

 

Trying a different tack, I reviewed the article Setting Up Authentication with OAuth 2.0, installed the OAuth Playground, and set up the Connected App with the OAuth settings that would be used to access my service.

 

Can someone point me to

  1. An Apex code sample for a method that can access an Apex REST service, using the Consumer Key and Secret (or whatever their OAuth 2.0 labels are now) created in the Connected App
  2. An Apex code sample (if different from 1., above ^) that can retrieve and make use of the sessionId described in the REST Basic Code sample
  3. The Salesforce Request Token URL (for the OAuth Playground)
  4. The Salesforce Access Token URL (for the OAuth Playground)

Any and/or all of the above will be greatly appreciated.

 

Thanks!

 

Duncan

(initially posted under Apex -- moving to API discussion thread)

Best Answer chosen by Admin (Salesforce Developers) 
Mohith Kumar ShrivastavaMohith Kumar Shrivastava

http://wiki.developerforce.com/page/Creating_REST_APIs_using_Apex_REST

 

The above article is a great article to start with apex rest .

 

Now if you want to get session Id you can use oauth 2.0.You can use REST Utility  of workbench https://workbench.developerforce.com/ for Testing any API instead of curl to make it faster

 

 

 

 

All Answers

amarcuteamarcute

Hi,

 

You can run separate "curl" command to get the sessionid.

 

curl https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d 
"client_id=XXXXXX.YYYYY_ZZZZZ" -d "client_secret=000099990000" -d "username=name@domain.com" -d 
"password=yourpasswordyoursecuritytoken"

when you get the response, take the session Id & replace it when you are forming the next "curl" request. you can also get the instance name (ex: na4, cs17 etc) from the response.

 

Check out this blog post 

 

http://blog.cloudspokes.com/2011/07/building-apis-with-salesforcecom-apex.html

Mohith Kumar ShrivastavaMohith Kumar Shrivastava

http://wiki.developerforce.com/page/Creating_REST_APIs_using_Apex_REST

 

The above article is a great article to start with apex rest .

 

Now if you want to get session Id you can use oauth 2.0.You can use REST Utility  of workbench https://workbench.developerforce.com/ for Testing any API instead of curl to make it faster

 

 

 

 

This was selected as the best answer