##Introduction
This repo contains a bash shell script example (Mac OSX tested) for using curl and the REST API to access salesforce.com data. I use this shell as a template for automating repetitive administrative tasks that are too inefficient in a user interface.
##Setup
- Download the .sh to your local machine. You can save it anywhere but I recommend saving it in your bash directory (e.g. /bin/bash) so you can simplify running the script.
- Change the permissions on the shell file (e.g. chmod +x curlREST.sh)
- Open your terminal app and navigate to the folder where you saved the shell script (e.g. cd ~/Desktop)
- To run the script, type './curlREST.sh' if the script is anywhere other than your bash directory (e.g. /bin/bash) and if it is in your bash directory, just type 'curlREST.sh'
- Enter your username
- Enter your password
- Enter your server instance (e.g. na1)
- If successful, you should get the results from a query of your accounts
##Important Parts
The script is commented, but the key aspects are described below.
Get the oauth2 response and store it (create your own connected app to get a new client id and secret)
response=`curl https://${instance}.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=<FILL_IN_CLIENT_ID>" -d "client_secret=<FILL_IN_CLIENT_SECRET>" -d "username=${username}" -d "password=${password}"`
Use some BASH_REMATCH magic to pull the access token substring out and store it. For more examples on BASH_REMATCH and testing RegEx in bash
if [[ "$response" =~ (\"access_token\"):\"(.+)\" ]]; then
access_token="${BASH_REMATCH[2]}"
Substitute your own REST API calls for this simple Account query
curl https://${instance}.salesforce.com/services/data/v29.0/query?q=Select+Id+From+Account+LIMIT+5 -H "Authorization: Bearer ${access_token}" -H "X-PrettyPrint:1"