Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@oh-sky
Last active December 11, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oh-sky/4617012 to your computer and use it in GitHub Desktop.
Save oh-sky/4617012 to your computer and use it in GitHub Desktop.
Facebook GraphAPIのOAuthAccessToken(Expire: 2month)をgetするためのプログラム。 $client_id,$client_secret,$redirect_uriに適切な文字列をセット。 $redirect_uriにてアクセスできる場所に設置し、ブラウザから$redirect_uriにアクセスすると、Expire:2monthのAccessTokenを入手できる。
<?php
//SETTINGS
//Facebook APP ID
$client_id = 'YOUR_APP_ID';
//Facebook APP SECRET
$client_secret = 'YOUR_APP_SECRET';
//WebURI to access this script
$redirect_uri = 'http://YOUR.DOMAIN/PATH/TO/this_script.php';
//Scopes U need
//Scopes: https://developers.facebook.com/docs/authentication/permissions/
$scope = 'publish_stream,manage_pages';
if(!isset($_GET['code'])){
//Get Code to get short expire token
header("Location: https://graph.facebook.com/oauth/authorize?client_id={$client_id}&redirect_uri={$redirect_uri}&scope={$scope}");
exit();
}else{
//Get short expire token
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id={$client_id}&client_secret={$client_secret}&redirect_uri={$redirect_uri}&code={$_GET['code']}");
if(preg_match('/access_token=([A-Za-z0-9]+)/',$response,$short_token)){
$short_token = $short_token[1];
}else{
exit("couldn't get short expire token<br>\n");
}
//Get 2month expire token
$response = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id={$client_id}&client_secret={$client_secret}&grant_type=fb_exchange_token&fb_exchange_token={$short_token}");
if(preg_match('/access_token=([A-Za-z0-9]+)/',$response,$exchange_token)){
echo ($exchange_token = $exchange_token[1])."<br>\n";
}else{
exit("couldn't get exchange token<br>\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment