├── README.md └── fb_token.sh /README.md: -------------------------------------------------------------------------------- 1 | fb_token 2 | ======== 3 | 4 | Facebook Access Token Generator 5 | 6 | This script helps to get facebook access tokens. Especially when you need a long-lasting token allowing an application to access facebook as a page. 7 | 8 | To run this script, you'll need your application's ID number and also its secret. The application developer can learn these values on https://developers.facebook.com/apps. 9 | 10 | The script will prompt you for the values it needs. Supply the app id and the script will attempt to launch a browser. Log into facebook and authorize the app. Then copy the URL shown in your browser and paste that entire string to the script. Your access token will be part of that URL. (You could optionally just paste the access token.) 11 | 12 | The script will prompt you for the application's secret which is needed to exchange the short-lived token for a longer-lived one. 13 | 14 | If the script succeeds, it will show you a long-lived token and a token for each of the pages your facebook account has permission to manage. 15 | 16 | These tokens can be used by Drupal for Facebook or similar tools that prompt you for a token. 17 | 18 | If you find this script useful, please consider making a donation to support this open source software. 19 | 20 | TODO 21 | ==== 22 | 23 | TODO: make input more user friendly. 24 | TODO: make output more user friendly. 25 | TODO: make scope (permissions) configurable. 26 | -------------------------------------------------------------------------------- /fb_token.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | 4 | echo -en "Paste either your Facebook application ID (step 1), or paste the URL copied from previous step (that would be step 2): " 5 | read X 6 | echo $X | grep "[^0-9]" > /dev/null 2>&1 7 | if [ "$?" -eq "0" ]; then 8 | # If the grep found something other than 0-9 9 | # then it's not an integer. 10 | Y=$X 11 | 12 | else 13 | echo $X 14 | 15 | # The grep found only 0-9, so it's an integer. 16 | url="https://www.facebook.com/dialog/oauth?client_id=$X&scope=manage_pages,publish_stream&response_type=token&redirect_uri=https://www.facebook.com/connect/login_success.html" 17 | 18 | echo -en "Opening a web browser. Please authorize your application in the browser. When your browser says \"Success\" please copy and paste the browser's URL." 19 | 20 | if [ -n "$BROWSER" ]; then 21 | $BROWSER $url 22 | elif which xdg-open > /dev/null; then 23 | xdg-open $url 24 | elif which gnome-open > /dev/null; then 25 | gnome-open $url 26 | # elif bla bla bla... 27 | else 28 | echo "Could not detect the web browser to use. Manually open a browser to this URL: $url" 29 | fi 30 | 31 | # It would be nice if the browser ran in the background and allowed user to paste value here. But on my system the script exits when the browser is launched. 32 | echo -en "Now paste the URL shown in your browser: " 33 | read Y 34 | fi 35 | 36 | echo " url: $Y" 37 | 38 | # This should parse just the access token from the facebook.com URL. 39 | token="$(echo $Y | sed -e's,.*access_token=\(.*\)&.*,\1,g')" 40 | 41 | echo " token: $token" 42 | 43 | app_json=$(curl "https://graph.facebook.com/app?access_token=$token") 44 | 45 | echo " app $app_json" 46 | 47 | id=$(echo "$app_json" | grep -Po '(?<="id":")[^"]*') 48 | name=$(echo "$app_json" | grep -Po '(?<="name":")[^"]*') 49 | 50 | if [ -n "$name" ]; then 51 | echo "Generating long-lasting tokens via application $name." 52 | else 53 | echo "Could not use token $token. \n$app_json \n\n" 54 | exit 1 55 | fi 56 | 57 | 58 | echo "\nNext, we need your application's secret. (hint see https://developers.facebook.com/apps/$id)" 59 | echo -en "Please paste secret here: " 60 | 61 | read secret 62 | 63 | exchange=$(curl "https://graph.facebook.com/oauth/access_token?client_id=$id&client_secret=$secret&grant_type=fb_exchange_token&fb_exchange_token=$token") 64 | echo " exchange got: $exchange" 65 | 66 | lt=$(echo $exchange | grep -Po '(?<=access_token=)[^&]*') 67 | 68 | if [ -n "$lt" ]; then 69 | echo "Your personal long-lasting token is $lt" 70 | else 71 | echo "Failed to get long-lasting token." 72 | echo "$exchange" 73 | exit 1 74 | fi 75 | 76 | accounts=$(curl "https://graph.facebook.com/me/accounts?access_token=$lt") 77 | #echo " accounts: $accounts" 78 | 79 | tokens=$(echo $accounts | grep -Po '"name":"[^"]*","access_token":"[^"]*"') 80 | 81 | if [ -n "$tokens" ]; then 82 | echo "Here are page-specific tokens you may choose. Copy the access token without quotations.\n" 83 | echo "$tokens" 84 | fi 85 | 86 | 87 | --------------------------------------------------------------------------------