Advertisement
Guest User

OAuth Generator

a guest
Feb 9th, 2015
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.74 KB | None | 0 0
  1. #filepath to store credentials
  2. my_creds=~/oauth_cache.txt
  3.  
  4. client_id=''
  5. client_secret=''
  6. redirectURI=''
  7.  
  8. if [ -s $my_creds ]; then
  9.   # if we already have a token stored, use it
  10.   . $my_creds
  11.   time_now=`date +%s`
  12. else
  13.   scope='https://www.googleapis.com/auth/analytics'
  14.   # Form the request URL
  15.   # http://goo.gl/U0uKEb
  16.   auth_url="https://accounts.google.com/o/oauth2/auth?client_id=$client_id&scope=$scope&response_type=code&redirect_uri=$redirectURI"
  17.  
  18.   echo "Please go to:"
  19.   echo
  20.   echo "$auth_url"
  21.   echo
  22.   echo "after accepting, enter the code you are given:"
  23.   read auth_code
  24.  
  25.   # swap authorization code for access and refresh tokens
  26.   # http://goo.gl/Mu9E5J
  27.   auth_result=$(curl -s https://accounts.google.com/o/oauth2/token \
  28.     -H "Content-Type: application/x-www-form-urlencoded" \
  29.     -d code=$auth_code \
  30.     -d client_id=$client_id \
  31.     -d client_secret=$client_secret \
  32.     -d redirect_uri=urn:ietf:wg:oauth:2.0:oob \
  33.     -d grant_type=authorization_code)
  34.   access_token=$(echo -e "$auth_result" | \
  35.                  ggrep -Po '"access_token" *: *.*?[^\\]",' | \
  36.                  awk -F'"' '{ print $4 }')
  37.   refresh_token=$(echo -e "$auth_result" | \
  38.                   ggrep -Po '"refresh_token" *: *.*?[^\\]",*' | \
  39.                   awk -F'"' '{ print $4 }')
  40.   expires_in=$(echo -e "$auth_result" | \
  41.                ggrep -Po '"expires_in" *: *.*' | \
  42.                awk -F' ' '{ print $3 }' | awk -F',' '{ print $1}')
  43.   time_now=`date +%s`
  44.   expires_at=$((time_now + expires_in - 60))
  45.   echo -e "access_token=$access_token\nrefresh_token=$refresh_token\nexpires_at=$expires_at" > $my_creds
  46.   echo -e "\n#Static data\nclientID=$client_id\nclientSecret=$client_secret\nredirectURI=$redirectURI" >> $my_creds
  47. fi
  48.  
  49. # if our access token is expired, use the refresh token to get a new one
  50. # http://goo.gl/71rN6V
  51. if [ $time_now -gt $expires_at ]; then
  52.   refresh_result=$(curl -s https://accounts.google.com/o/oauth2/token \
  53.    -H "Content-Type: application/x-www-form-urlencoded" \
  54.    -d refresh_token=$refresh_token \
  55.    -d client_id=$client_id \
  56.    -d client_secret=$client_secret \
  57.    -d grant_type=refresh_token)
  58.   access_token=$(echo -e "$refresh_result" | \
  59.                  ggrep -Po '"access_token" *: *.*?[^\\]",' | \
  60.                  awk -F'"' '{ print $4 }')
  61.   expires_in=$(echo -e "$refresh_result" | \
  62.                ggrep -Po '"expires_in" *: *.*' | \
  63.                awk -F' ' '{ print $3 }' | awk -F',' '{ print $1 }')
  64.   time_now=`date +%s`
  65.   expires_at=$(($time_now + $expires_in - 60))
  66.   echo -e "access_token=$access_token\nrefresh_token=$refresh_token\nexpires_at=$expires_at" > $my_creds
  67.   echo -e "\n#Static data\nclientID=$client_id\nclientSecret=$client_secret\nredirectURI=$redirectURI" >> $my_creds
  68. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement