├── README.md ├── curlREST.sh └── terminalExample.png /README.md: -------------------------------------------------------------------------------- 1 | curlREST 2 | ======== 3 | 4 | ##Introduction 5 | 6 | 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. 7 | 8 | ##Setup 9 | 10 | 1. 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. 11 | 2. Change the permissions on the shell file (e.g. chmod +x curlREST.sh) 12 | 3. Open your terminal app and navigate to the folder where you saved the shell script (e.g. cd ~/Desktop) 13 | 4. 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' 14 | 5. Enter your username 15 | 6. Enter your password 16 | 7. Enter your server instance (e.g. na1) 17 | 8. If successful, you should get the results from a query of your accounts 18 | 19 | ![screenshot](https://raw.githubusercontent.com/atorman/curlREST/master/terminalExample.png) 20 | 21 | ##Important Parts 22 | 23 | The script is commented, but the key aspects are described below. 24 | 25 | Get the oauth2 response and store it (create your own [connected app](http://help.salesforce.com/apex/HTViewHelpDoc?id=connected_app_create.htm&language=en_US "Creating a Connected App help topic") to get a new client id and secret) 26 | 27 | response=`curl https://${instance}.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=" -d "client_secret=" -d "username=${username}" -d "password=${password}"` 28 | 29 | 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](http://robots.thoughtbot.com/the-unix-shells-humble-if "Unix Shells Humble If") 30 | 31 | if [[ "$response" =~ (\"access_token\"):\"(.+)\" ]]; then 32 | access_token="${BASH_REMATCH[2]}" 33 | 34 | Substitute your own REST API calls for this simple Account query 35 | 36 | 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" 37 | 38 | -------------------------------------------------------------------------------- /curlREST.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #you may need to change permissions on the shell file (e.g. chmod +x curlREST.sh) to run it (e.g. ./curlREST.sh) 4 | 5 | #prompt the user to enter their username or uncomment #username line for testing purposes 6 | read -p "Please enter username (and press ENTER): " username 7 | 8 | #prompt the user to enter their password 9 | read -s -p "Please enter password (and press ENTER): " password 10 | 11 | #prompt the user to enter their instance end-point 12 | echo 13 | read -p "Please enter instance (e.g. na1) for the loginURL (and press ENTER): " instance 14 | 15 | #prompt the user to enter their clientid 16 | read -p "Please enter Salesforce connected app client id (and press ENTER): " client_id 17 | 18 | #prompt the user to enter their clientsecret 19 | read -s -p "Please enter Salesforce connected app client secret (and press ENTER): " client_secret 20 | 21 | #get the oauth2 response and store it (create your own connected app to get a new client id and secret - https://na1.salesforce.com/help/pdfs/en/salesforce_identity_implementation_guide.pdf) 22 | response=`curl https://${instance}.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=${client_id}" -d "client_secret=${client_secret}" -d "username=${username}" -d "password=${password}"` 23 | 24 | #uncomment to check response json 25 | echo "response: {$response}" 26 | 27 | #test regular expression for an access token 28 | pattern='"access_token":"([^"]*)"' 29 | if [[ $response =~ $pattern ]]; then 30 | 31 | #use some BASH_REMATCH magic to pull the access token substring out and store it - see http://robots.thoughtbot.com/the-unix-shells-humble-if for examples 32 | access_token="${BASH_REMATCH[1]}" 33 | 34 | #uncomment to check token results 35 | echo "token: ${access_token}" 36 | 37 | #now run whatever REST API query, insert, delete, etc... you want 38 | 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" 39 | else 40 | #whoops - what happened? 41 | echo "something went terribly wrong :(" 42 | fi 43 | -------------------------------------------------------------------------------- /terminalExample.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorman/curlREST/78129ca904b2b645fbc538134e482b3173859f2e/terminalExample.png --------------------------------------------------------------------------------