Skip to main content

Force.com REST API getting token with curl

Force REST API usage with curl is really well documented, there are at least two official HowTO's, but both of the assume that you will get your OATH token with Java. Below I've described steps required to get the token and verify your token with curl.

1. Retrieve token

curl -v -k https://DOMAIN.salesforce.com/services/oauth2/token 
-d "grant_type=password" -d "client_id=CONSUMER_KEY" 
-d "client_secret=CONSUMER_SECRET" -d "username=USERNAME" 
-d "password=USER_PASSWORDSECURITY_TOKEN"

where:
  • DOMAIN - is your salesforce domain (e.g. na9)
  • CONSUMER_KEY, CONSUMER_SECRET - as taken from App Setup -> Develop -> Remote Access
  • USERNAME, USER_PASSWORD, SECURITY_TOKEN - properties for the user that you want to connect with. Note that USER_PASSWORD and SECURITY_TOKEN are concatenated.
Note that I used -k parameter in curl which results in the SSL certificate validation skipped (otherwise you need to add it or the root certificate to your key store). 

In the response you'll get for example (for na9.salesforce.com):

* About to connect() to na9.salesforce.com port 443 (#0)
*   Trying 96.43.145.44... connected
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using RC4-MD5
* Server certificate:
*        subject: C=US; ST=California; L=San Francisco; O=Salesforce.com, Inc.;
OU=Applications; CN=na9.salesforce.com
*        start date: 2010-12-16 00:00:00 GMT
*        expire date: 2012-12-15 23:59:59 GMT
*        common name: na9.salesforce.com (matched)
*        issuer: C=US; O=VeriSign, Inc.; OU=VeriSign Trust Network; OU=Terms of
use at https://www.verisign.com/rpa (c)10; CN=VeriSign Class 3 International Ser
ver CA - G3
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
> POST /services/oauth2/token HTTP/1.1
> User-Agent: curl/7.23.1 (x86_64-pc-win32) libcurl/7.23.1 OpenSSL/0.9.8r zlib/1
.2.5
> Host: na9.salesforce.com
> Accept: */*
> Content-Length: 212
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 212 out of 212 bytes
< HTTP/1.1 200 OK
< Server:
< Content-Type: application/json; charset=UTF-8
< Transfer-Encoding: chunked
< Date: Tue, 12 Jun 2012 05:03:02 GMT
<
{"id":"https://login.salesforce.com/id/00DE0000000YDIrMAO/005E00000017YfmIAE","i
ssued_at":"1339477382420","instance_url":"https://na9.salesforce.com","signature
":"jpqWF7t6KtWszMCuzCcXOkzqUxTw+7bSv0X5G3+jHBA=","access_token":"00DE0000000YDIr
!AQYAQEfgbWngXrxWpagPgEMckQCD1C6K5K9KoUVRUr0oLt.BN4rufk1pyGskMxdbxuAY5OkdAY.J36e
_Iscm_lc699oIILoE"}* Connection #0 to host na9.salesforce.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):

The token that you're looking for in this case is:  00DE0000000YDIr!AQYAQEfgbWngXrxWpagPgEMckQCD1C6K5K9KoUVRUr0oLt.BN4rufk1pyGskMxdbxuAY5OkdAY.J36e_Iscm_lc699oIILoE

2. Test if everything works

The simplest way to test if the token works is list objects that you have available for given token. You can do it with:

curl -k https://na9.salesforce.com/services/data/v20.0/sobjects/ 
-H "Authorization:OAuth TOKEN" -H "X-PrettyPrint:1"

If you get a long list of JSON formatted objects - you're good to continue as per information in the official force.com REST API developer guide:
http://www.salesforce.com/us/developer/docs/api_rest/index.htm

Additional reading describing OATH usage:
https://na9.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm

Comments

  1. This concept is a good way to enhance the knowledge.thanks for sharing. please keep it up salesforce Online Course Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

Eclipse + EGit - "The authenticity of host ... can't be established" challenge

Recently while writing new Android code I decided that it's the highest time to have a Git repository not only on my hard drive, but also safe in the Internet. After quick search and finding out that I have accounts at almost every popular service that provides Git hosting, I figured out that one that covers everything I need (wiki, bug tracking, code hosting, forums) is the good old sourceforge. I used it also with no problems few months ago on another mobile project, so I was hoping that pushing code there will be a piece of cake. But then when I tried to do it (after configuring the project on the sourceforge site), I got very interesting error: ssh://USER@git.code.sf.net:22: org.eclipse.jgit.transport.CredentialItem$YesNoType:The authenticity of host 'git.code.sf.net' can't be established. RSA key fingerprint is 86:7b:1b:12:85:35:8a:b7:98:b6:d2:97:5e:96:58:1d. Are you sure you want to continue connecting? In theory it's nothing bad, you press the "Y

How to make Logitech Trackball Marble Wheel work

If you bought Trackball Marble from Logitech, the first challenge you encounter is probably related to the lack of the wheel button. Unfortunately the software provided with the device for Windows doesn't help (neither Universal or Auto Search aren't really working as I was expecting). Internet suggests mostly one option, app called Marble Mouse Scroll Wheel http://marble-mouse-scroll-wheel.software.informer.com/ To some extent it works, but I wasn't able to make it work in google maps or in picture viewer. Moreover setting where crashing very often (I am running windows 7 64 bits). Fortunately there is a way to have a semi-wheel button behavior with this trackball, but with a different software - X-Mouse Button Control: http://www.highrez.co.uk/downloads/XMouseButtonControl.htm Setup Mouse button 4 and 5 as wheel up and wheel down. Then also update Logitech SetPoint settings to replace the behavior of those button to default. Voila - now you can emulate wheel

HTTPS for dummies - so how HTTPS really works in 5 mins

What is HTTPS? HTTPS (HyperText Transfer Protocol Secure) is a way of transferring data over internet in a secure manner. It's achieved through adding SSL (Secure Socket Layer)/TLS (Transport Layer Security) on top of standards HTTP. What HTTPS gives us? End-2-end encryption of data - from the browser to the server and back = even if someone reads the data you are sending, they will not be able to understand anything out of it Confirmation of the identity of the website we are accessing = you are sure that the website that looks like your bank is actually your bank (and not a phishing website) How does it work? First you need a pair of SSL certificates: One installed in your web browser (in most cases shipped together with your browser, provided by one of so-called trusted Certificate Authorities) One installed on the website (which is acquired by the website owner) Each of those SSL certificates includes the following information:  Public information: name of