Sunday, November 22, 2015

Generating Access Token for Connected App using CURL

When you have designed a REST API using apex, to provide the authentication to the requester we will be creating a Connected App in salesforce .In this post we will being learning how to create a Connected App and how to get the AcccessToken using the CURL.

Creating the Connected App 

1.Goto Setup--> Create --> App--> click on Connected Apps (as shown as below)




























2. Fill all the manadtory fields(Connected App Name,Contact Email) as heigligted as below image


























3. For authentication purpose we are using here OAuth .So,click on Enable OAuth Settings checkbox 

    i) Fill the CallBack URL with below values based on instance
       
         Prod or Developer Edition =  https://login.salesforce.com/services/oauth2/callback
         For Sandboxes                   =  https://test.salesforce.com/services/oauth2/callback

  ii)In Selected OAuth Scopes section move Full access(full) value from Available OAuth Scopes to Selected OAuth Scopes (Plz refer the below image for more info)




























4.Click on Save and your connected app would populate with Consumer Key and Consumer Secret as heightened in the below image


























Getting the Access Token using the CURL


Testing RESTful webservices on Windows can be done outside of the browser using an open source command tool called cURL.

1.you can download the CURL from the below link based on your operating systems.
     a)  CURL download link
     b)  For Windows 32 bit (safer choice)
     c)  For Window 64 bit

2. For windows download Zip file from the above mention links (b and c).

3. Extract the zip file copy the curl.exe file and paste in the c:\windows\system32

4. Installation of CURL is finished.

5. To test the installation execute the command as given in image























Syntax to get the Access Token 

1.Open command promt and execute the below command

 curl --form client_id=<Your client id/Consumer Key>  --form client_secret=<Your Secret ID/Consumer Secret> --form grant_type=password --form username=<Your User Name>  --form password=<Your Password+SecurityToken> -k https://<Your Domain/Instance Name>.salesforce.com/services/oauth2/token

Example

curl --form client_id=3MVG9Y6d_Btp4xp7.7cBQRNzmybjPJDdJ8B1jneA51uSo_o_5Vwkz --form client_secret=596012939950 --form grant_type=password --form username=test@gmail.com --form password=ilovecodeYlM4IQZjX1TiDAW -k https://cs10.salesforce.com/services/oauth2/token

If command is successfully executed you will receive below response with Access Token

{"access_token":"00D90000000ewis!AQ0A81cE1b4zMyVTJ0lEuk7vxny4WDtUAuEQC1MqOgp6UpIWQT6.F8CQOc4u5b15.y6",
  "instance_url":"https://cs10.salesforce.com",
  "id":"https://login.salesforce.com/id/00D90077777ewisEAA/005900111100jjJ7AAI",
  "token_type":"Bearer",
  "issued_at":"1448333666867",
  "signature":"3vjbiglhrQRMA3KqCiabcBqnoqs4TNdmzyQDlV/M1Aw="}


Thanks for visiting...Enjoy........