Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # This script generates Zoho API tokens. It requires 'jq' to parse the output.
- # If you have any questions, you can find me as xrobau pretty much everywhere
- # (gmail, twitter, facebook, etc)
- #
- # Note you can provide params if you want:
- # ./gentoken.sh email@addr 1000.clientid bbbbbSECRETbbbbb http://uri
- #
- # Also - the URI does not need to be valid. It just needs to be a URI, and
- # it does not need to match the domain. I use 'example.com' and 'http://uri'
- #
- # --xrobau
- JQCHECK=$(echo '{"herp":"derp"}' | jq -r .herp 2>&1)
- if [ "$JQCHECK" != "derp" ]; then
- echo "Something is wrong with jq - is it not installed?"
- exit
- fi
- mkdir -p config
- echo -e "*** API Credentials Required ***\n"
- if [ "$1" == "" ]; then
- echo "Please enter your Email Address as registered inside zoho."
- echo "This is what is on https://accounts.zoho.com/home#profile/personal"
- echo -n "Email: "
- read EMAIL
- else
- echo Using $1 as EMAIL
- EMAIL=$1
- fi
- echo -e "Go to https://accounts.zoho.com/developerconsole\n"
- echo "If there is not already a Client ID there, create one using a valid"
- echo "domain and redirect URL (you don't need to use them yet, but you may"
- echo "need to use them in the future). For example, 'Test API', 'example.com',"
- echo "and 'https://api.example.com/callback'. When you have created it, you"
- echo -e "will be presented with a Client ID and secret.\n"
- echo "If there is one there already, click on the three dots on the right and"
- echo -e "select 'Edit'. You will see the Client ID and secret there.\n"
- if [ "$2" == "" ]; then
- echo -n "Please paste the Client ID here: "
- read CLIENTID
- else
- echo Using $2 as CLIENTID
- CLIENTID=$2
- fi
- if [ "$3" == "" ]; then
- echo -n "Please paste the Secret here: "
- read SECRET
- else
- echo Using $3 as SECRET
- SECRET=$3
- fi
- if [ "$4" == "" ]; then
- echo -n "Please paste the URL you entered: "
- read URL
- else
- echo Using $4 as URL
- URL=$4
- fi
- echo -e "\nNow close that window and click on the three dots AGAIN, but this time"
- echo -e "select 'Self Client'. Paste the following line into 'Scope' and click 'View Code':\n"
- echo -e "ZohoBooks.fullaccess.all,ZohoCRM.modules.custom.all,ZohoCRM.modules.contacts.all,ZohoCRM.modules.ALL,ZohoCRM.settings.ALL,ZohoCRM.users.ALL,ZohoCRM.org.ALL,ZohoCRM.settings.functions.all,ZohoCRM.functions.execute.read,ZohoCRM.functions.execute.create,ZohoCRM.settings.layout_rules.read,ZohoCRM.notifications.all,aaaserver.profile.all\n"
- echo -n "Paste the Generated Code here: "
- read CODE
- echo -n "Trying to get a refresh token from Zoho..."
- # If something is going wrong, uncomment this for debugging
- # set -x
- RES=$(curl --silent -L -X POST \
- --data-urlencode "code=$CODE" \
- --data-urlencode "client_id=$CLIENTID" \
- --data-urlencode "redirect_uri=$URL" \
- --data-urlencode "client_secret=$SECRET" \
- --data-urlencode "grant_type=authorization_code" \
- --data-urlencode "access_type=offline" \
- https://accounts.zoho.com/oauth/v2/token)
- if [[ $RES == *refresh_token* ]]; then
- echo -n Found refresh token:
- TOKEN=$(echo $RES | jq -r .refresh_token)
- echo $TOKEN
- else
- echo "It failed. Here's the output, go ask zoho why. (Don't change the 'validity time' - it breaks)"
- echo $RES
- exit
- fi
- echo "EMAIL=$EMAIL" > config/prod
- echo "CLIENTID=$CLIENTID" >> config/prod
- echo "SECRET=$SECRET" >> config/prod
- echo "URL=$URL" >> config/prod
- echo "TOKEN=$TOKEN" >> config/prod
- cat config/prod > config/sandbox
- echo "SANDBOX=true" >> config/sandbox
- echo "Credentials successfully stored in config/prod and config/sandbox"
Add Comment
Please, Sign In to add comment