xrobau

Zoho Generate API Key

Nov 14th, 2019
2,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.76 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script generates Zoho API tokens. It requires 'jq' to parse the output.
  4. # If you have any questions, you can find me as xrobau pretty much everywhere
  5. # (gmail, twitter, facebook, etc)
  6. #
  7. # Note you can provide params if you want:
  8. # ./gentoken.sh email@addr 1000.clientid bbbbbSECRETbbbbb http://uri
  9. #
  10. # Also - the URI does not need to be valid. It just needs to be a URI, and
  11. # it does not need to match the domain. I use 'example.com' and 'http://uri'
  12. #
  13. # --xrobau
  14.  
  15. JQCHECK=$(echo '{"herp":"derp"}' | jq -r .herp 2>&1)
  16. if [ "$JQCHECK" != "derp" ]; then
  17.         echo "Something is wrong with jq - is it not installed?"
  18.         exit
  19. fi
  20.  
  21. mkdir -p config
  22.  
  23. echo -e "*** API Credentials Required ***\n"
  24.  
  25. if [ "$1" == "" ]; then
  26.         echo "Please enter your Email Address as registered inside zoho."
  27.         echo "This is what is on https://accounts.zoho.com/home#profile/personal"
  28.         echo -n "Email: "
  29.         read EMAIL
  30. else
  31.         echo Using $1 as EMAIL
  32.         EMAIL=$1
  33. fi
  34.  
  35. echo -e "Go to https://accounts.zoho.com/developerconsole\n"
  36.  
  37. echo "If there is not already a Client ID there, create one using a valid"
  38. echo "domain and redirect URL (you don't need to use them yet, but you may"
  39. echo "need to use them in the future). For example, 'Test API', 'example.com',"
  40. echo "and 'https://api.example.com/callback'.  When you have created it, you"
  41. echo -e "will be presented with a Client ID and secret.\n"
  42.  
  43. echo "If there is one there already, click on the three dots on the right and"
  44. echo -e "select 'Edit'. You will see the Client ID and secret there.\n"
  45.  
  46. if [ "$2" == "" ]; then
  47.         echo -n "Please paste the Client ID here: "
  48.         read CLIENTID
  49. else
  50.         echo Using $2 as CLIENTID
  51.         CLIENTID=$2
  52. fi
  53.  
  54. if [ "$3" == "" ]; then
  55.         echo -n "Please paste the Secret here: "
  56.         read SECRET
  57. else
  58.         echo Using $3 as SECRET
  59.         SECRET=$3
  60. fi
  61.  
  62. if [ "$4" == "" ]; then
  63.         echo -n "Please paste the URL you entered: "
  64.         read URL
  65. else
  66.         echo Using $4 as URL
  67.         URL=$4
  68. fi
  69.  
  70. echo -e "\nNow close that window and click on the three dots AGAIN, but this time"
  71. echo -e "select 'Self Client'. Paste the following line into 'Scope' and click 'View Code':\n"
  72. 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"
  73. echo -n "Paste the Generated Code here: "
  74. read CODE
  75. echo -n "Trying to get a refresh token from Zoho..."
  76.  
  77. # If something is going wrong, uncomment this for debugging
  78. # set -x
  79.  
  80. RES=$(curl --silent -L -X POST \
  81.         --data-urlencode "code=$CODE" \
  82.         --data-urlencode "client_id=$CLIENTID" \
  83.         --data-urlencode "redirect_uri=$URL" \
  84.         --data-urlencode "client_secret=$SECRET" \
  85.         --data-urlencode "grant_type=authorization_code" \
  86.         --data-urlencode "access_type=offline" \
  87.         https://accounts.zoho.com/oauth/v2/token)
  88.  
  89. if [[ $RES == *refresh_token* ]]; then
  90.         echo -n Found refresh token:
  91.         TOKEN=$(echo $RES | jq -r .refresh_token)
  92.         echo $TOKEN
  93. else
  94.         echo "It failed. Here's the output, go ask zoho why. (Don't change the 'validity time' - it breaks)"
  95.         echo $RES
  96.         exit
  97. fi
  98.  
  99. echo "EMAIL=$EMAIL" > config/prod
  100. echo "CLIENTID=$CLIENTID" >> config/prod
  101. echo "SECRET=$SECRET" >> config/prod
  102. echo "URL=$URL" >> config/prod
  103. echo "TOKEN=$TOKEN" >> config/prod
  104. cat config/prod > config/sandbox
  105. echo "SANDBOX=true" >> config/sandbox
  106. echo "Credentials successfully stored in config/prod and config/sandbox"
Add Comment
Please, Sign In to add comment