Created
June 8, 2016 20:47
-
-
Save zauberstuhl/ef6419681f9d6aae9713ec9b40cf4fc3 to your computer and use it in GitHub Desktop.
Diaspora API - Register a new application/client and retrieve an access token for your pod
This file contains 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 | |
echo -n "Name your client (MyBot): " | |
read client_name | |
echo -n "Define your pod address (with https://): " | |
read domain | |
cookie_file="/tmp/.cookie" | |
openid_url="$domain/api/openid_connect" | |
curl_options="-# --cookie-jar $cookie_file --cookie $cookie_file" | |
resp=$(curl $curl_options $openid_url/clients \ | |
-d "client_name=$client_name&redirect_uris[]=$domain") | |
client_id=$(echo $resp |perl -lpe 's/.*"client_id":"([^"]+?)".*$/$1/g') | |
client_secret=$(echo $resp |perl -lpe 's/.*"client_secret":"([^"]+?)".*$/$1/g') | |
if [ "$(echo -n $client_id |wc -c)" != "32" ]; | |
then | |
echo "Your client_id is invalid!" | |
exit 1 | |
fi | |
if [ "$(echo -n $client_secret |wc -c)" != "64" ]; | |
then | |
echo "Your client_secret is invalid!" | |
exit 1 | |
fi | |
params="client_id=$client_id&redirect_uri=$domain&response_type=code&scope=openid%20read%20write&nonce=hi&state=hi" | |
resp=$(curl $curl_options -L "$openid_url/authorizations/new?$params") | |
authenticity_token=$(echo $resp |perl -lpe 's/.*name="authenticity_token"\svalue="([^"]+?)".*$/$1/g') | |
if [ "$(echo -n $authenticity_token |wc -c)" != "88" ]; | |
then | |
echo "Your authenticity_token is invalid!" | |
exit 1 | |
fi | |
echo -n "Username ($domain): " | |
read username | |
echo -n "Password ($domain): " | |
read -s password | |
echo | |
resp=$(curl $curl_options -L -H "X-CSRF-TOKEN: $authenticity_token" \ | |
-d "user[username]=$username&user[password]=$password&user[remember_me]=1" \ | |
"$domain/users/sign_in") | |
authenticity_token=$(echo $resp |perl -lpe 's/.*name="authenticity_token"\svalue="([^"]+?)".*$/$1/g') | |
if [ "$(echo -n $authenticity_token |wc -c)" != "88" ]; | |
then | |
echo "Your authenticity_token is invalid!" | |
exit 1 | |
fi | |
echo -en "\n\nClient name: $client_name\nDo you want to register this application (y/N)? " | |
read confirmation | |
if [ "${confirmation,,}" != "y" ]; then | |
echo "Abort." | |
exit 1 | |
fi | |
resp=$(curl $curl_options -H "X-CSRF-TOKEN: $authenticity_token" \ | |
"$openid_url/authorizations" \ | |
-d "authenticity_token=$authenticity_token&commit=Approve&approve=true") | |
auth_code=$(echo $resp |perl -lpe 's/.*\?code=([^"]+?)".*$/$1/g') | |
if [ "$(echo -n $auth_code |wc -c)" != "64" ]; | |
then | |
echo $auth_code | |
echo "Your auth_code is invalid!" | |
exit 1 | |
fi | |
resp=$(curl $curl_options "$openid_url/access_tokens" \ | |
-d "grant_type=authorization_code&client_id=$client_id&client_secret=$client_secret&redirect_uri=$domain&code=$auth_code") | |
echo -e "\n\nClient ID: $client_id\nClient secret: $client_secret\nAuth code: $auth_code\n\n" | |
echo $resp | |
# clean up | |
rm $cookie_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment