this Makefile works as of 2015-10-29. steps 2 and 3 give only a two-month token, but the page access token given in the final step shows in the debugger as "Expires: Never". this answer draws upon the work of several others, and is provided in the hopes that it will simplify things for developers regardless of preferred programming language.
before using this, you need to put your existing page ID, app ID, and app secret, in that order, in your ~/.netrc file as follows: machine graph.facebook.com login 123456 account 234567 password 345678
also before using this, login to Facebook with w3m, clicking "Keep me logged in".
MACHINE := graph.facebook.com
PAGE_ID := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$4}' $(HOME)/.netrc)
APP_ID := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$6}' $(HOME)/.netrc)
APP_SECRET := $(shell awk '$$2 ~ /^$(MACHINE)$$/ {print $$8}' $(HOME)/.netrc)
PERMISSIONS := manage_pages,publish_actions,publish_pages
FB := https://www.facebook.com
GRAPH := https://$(MACHINE)
CODE ?=
TOKEN ?=
TWOMONTHTOKEN ?=
BROWSER ?= w3m -dump
REDIRECT := http://jc.unternet.net/test.cgi
CLIENT_SIDE := $(FB)/dialog/oauth?client_id=$(APP_ID)&redirect_uri=$(REDIRECT)
CLIENT_SIDE := $(CLIENT_SIDE)&scope=$(PERMISSIONS)&response_type=code
SERVER_SIDE := $(GRAPH)/oauth/access_token?client_id=$(APP_ID)
SERVER_SIDE := $(SERVER_SIDE)&redirect_uri=$(REDIRECT)
SERVER_SIDE := $(SERVER_SIDE)&client_secret=$(APP_SECRET)&code=$(CODE)
LONG_LIVED := $(GRAPH)/oauth/access_token?client_id=$(APP_ID)
LONG_LIVED := $(LONG_LIVED)&client_secret=$(APP_SECRET)
LONG_LIVED := $(LONG_LIVED)&grant_type=fb_exchange_token
LONG_LIVED := $(LONG_LIVED)&fb_exchange_token=$(TOKEN)
ACCOUNTS := $(GRAPH)/me/accounts?access_token=$(TWOMONTHTOKEN)
export
env:
env
@echo Usage: make code
@echo ' ' make CODE=codefrompreviousstep token
@echo ' ' make TOKEN=tokenfrompreviousstep longterm
@echo ' ' make TWOMONTHTOKEN=tokenfrompreviousstep accounts
@echo Then edit '$$HOME/.netrc' replacing password with page token
code:
$(BROWSER) "$(CLIENT_SIDE)"
token:
$(BROWSER) "$(SERVER_SIDE)"
longterm:
$(BROWSER) "$(LONG_LIVED)"
accounts:
$(BROWSER) $(ACCOUNTS)
it turns out in many cases the first step fails with w3m. in that case, install another browser such as firefox; ssh -X
to your server if the script is remotely hosted; and use make BROWSER=firefox code
instead. the following steps should work with w3m as shown.
note: if cutting-and-pasting this Makefile, make sure to replace the 4-space indentations with proper tabs.