We have created a Connected App in Part I , so now we need to make requests through the rest api.
So as I mentioned earlier we need grant_type , client_id , client_secret , username , password parameters to authenticate with the salesforce in order to call to the endpoint https://login.salesforce.com/services/oauth2/token. And that request should be a POST request.
What will happen here is once we send that POST request with the parameters if it is a successful authentication it will return a access token.This access token is same as the session id. After that each and every request we need to attach the access token in the header. Behind the seen salesforce validate the access token and if it is validate it will returning the request data. if the validate fails salesforce returning 401 unauthorized error.
As you know session has expire time. As same as salesforce access token has expire time.You can change access token settings by clicking the Connected App's manage button then the edit button. After that you will be see below screen.
In here you can customize the Refresh Token Policy and it including the refresh token time out and many more.
If the access token expires , then that access token is no more valid , so we need to request for a refresh token via OAuth end point along with the old access token/refresh token.
Once you authenticate with the salesforce you will be able to do lots of things via the rest api. As a example we can call GET , POST , PUT, PATCH , DELETE , HEAD http methods on the rest ap. Below I mentioned some get services exposed in saleforce REST API.
So Now I will show you how to call for a salesforce rest api through the post man chrome extension. Here I choose post man because it is really easy to test rest api's. There are many more great tools to make the requests.
- you can download post man rest client by clicking following link. https://chrome.google.com/webstore/detail/postman-rest-client-short/mkhojklkhkdaghjjfdnphfphiaiohkef?hl=en
- Once you download then go the postman page by clicking post man extension. And paste below URL in the place holder called Enter request URL here and set http method to POST.
- Then click URL params button and add following parameters.
client_id : 'Connected app's Consumer Key'
client_secret : 'Connected app's Consumer Secreat'
username : 'your salesforce username'
password : 'your salesforce password + security token'
- Please see below screen to get client_id and client_secret. You need to get security token by clicking My settings -> Personal -> Reset Security Token. This will send the security token by email.
- Now you got everything to call to salesforce OAuth endpoint. So you need to set all parameters as below screen in post man.
- So click send , If the endpoint name and method , parameters are correct you will receive an access token.
- Common Issues :
- When you coping client-secret or client_id you will copy the text with some spaces. So make sure
client-secret or client_id has no spaces. - Didn't append security token with the password. Make sure you have paste security token after the password in password parameter.
- Didn't change the http method to POST. Make sure you are sending a POST http method
- I will explain how to get salesforce data through the rest call in the Part III
Comments
Post a Comment