Created
April 11, 2013 02:51
-
-
Save joaoneto/5360269 to your computer and use it in GitHub Desktop.
Authorize and get token
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
COOKIE_TMP_FILE=".tmp_cookie" | |
USER_EMAIL="[email protected]" | |
USER_PASSWORD="123" | |
CLIENT_ID="XXXXXXXXXXXXXXXXXXXXXXXX" | |
CLIENT_SECRET="abc123" | |
REDIRECT_URI="http://localhost:3000" | |
# Remove tmp cookies | |
rm -rf "$COOKIE_TMP_FILE" | |
# LOGIN | |
LOGIN=$(curl http://localhost:3000/login -b $COOKIE_TMP_FILE -c $COOKIE_TMP_FILE -d "email=$USER_EMAIL&password=$USER_PASSWORD") | |
# Parse user_id from login | |
# USER_ID=$(echo $LOGIN | grep -Po '(?<="id": ")[^"]*') | |
# AUTHORIZATION | |
AUTHORIZATION=$(curl -b $COOKIE_TMP_FILE "http://localhost:3000/authorize/?client_id=$CLIENT_ID&response_type=code&redirect_uri=$REDIRECT_URI") | |
TRANSACTION_ID=$(echo $AUTHORIZATION | grep -Po '(?<="transactionID": ")[^"]*') | |
# Workaround to parse code | |
WORKAROUND_CODE=$(curl http://localhost:3000/authorize/decision -b $COOKIE_TMP_FILE -d "transaction_id=$TRANSACTION_ID&client_id=$CLIENT_ID") | |
CODE=$(echo $WORKAROUND_CODE | grep -Po '(?<=code\=)[^=]*') | |
# Token | |
TOKEN=$(curl http://localhost:3000/token -d "code=$CODE&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&redirect_uri=$REDIRECT_URI&grant_type=authorization_code") | |
ACCESS_TOKEN=$(echo $TOKEN | grep -Po '(?<="access_token":")[^"]*') | |
# Get protected resource | |
echo $(curl -b $COOKIE_TMP_FILE -H "Authorization: Bearer $ACCESS_TOKEN" -v "http://localhost:3000/info") | |
# echo $(curl -b $COOKIE_TMP_FILE -v "http://localhost:3000/info/?access_token=$ACCESS_TOKEN") | |
# Logout | |
echo $(curl http://localhost:3000/logout -b $COOKIE_TMP_FILE -d "") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment