Reputation: 2700
I tried use easy curl to get facebook code
and access_token
via curl. So that I can post message to my wall. I know sdk
, but if I only use easy curl
way?
some fault return:
Method Not Implemented
Invalid method in request
Here is my code. BY THE WAY: How to remember a access_token
if it is still valid in 1 hour period time? Thanks.
$code_url = "https://graph.facebook.com/oauth/authorize?client_id=".$app_id."&redirect_uri=".urlencode($canvas_page_url)."&type=client_cred&display=page&scope=user_photos,publish_stream,read_stream,user_likes";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$code_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$fb_code = curl_exec($ch); // get code
curl_close($ch);
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&redirect_uri=".urlencode($canvas_page_url)."&code=".$fb_code."";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$token_url);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($curl, CURLOPT_HEADER ,0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER ,1);
$result = curl_exec($curl);
curl_close($curl);
echo $result; //get token for post to wall
EDIT:
$app_id = "14XXXXXXXX";
$app_secret = "77XXXXXXXXXXXXXXXXXXXXXXX";
function get_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => $app_id,
"client_secret" => $app_secret
);
return str_replace('access_token=', '', post_url($url, $token_params));
}
$token = get_app_access_token($app_id,$app_secret);
echo $token;
Upvotes: 4
Views: 16297
Reputation: 2003
They really ought to make this clearer on the FB guide, but here's what worked for me. Unlike what a lot of people are saying online, you do NOT do a GET curl call but you still do a POST call.
So, the URL you post to will be "https://graph.facebook.com/oauth/access_token?"
And then, your POST variables that you'll feed CURLOPT_POSTFIELDS will be something like:
"client_id=YOUR_CLIENT_ID&redirect_uri=" . urlencode("YOUR_URL") . "&client_secret=YOUR_CLIENT_SECRET&code=YOUR_RECEIVED_CODE"
I was literally trying for at least 1.5 hours trying to get this to work. Every time I went to the URL in a browser it was fine, and every time I tried to run a GET curl command, or a POST command where all the variables were still in the url (and not specified in POSTFIELDS) it failed. It was really annoying.
Upvotes: 2
Reputation: 1335
Try these helper methods which will make a cURL call to the graph API and return an application access_token.
This function takes a url and an array of parameters and makes a POST via cURL:
function post_url($url, $params) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params, null, '&'));
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
This function takes your app ID and secret provided in the developer app and returns an active access_token for your app:
function get_app_access_token($app_id, $secret) {
$url = 'https://graph.facebook.com/oauth/access_token';
$token_params = array(
"type" => "client_cred",
"client_id" => $app_id,
"client_secret" => $secret
);
return str_replace('access_token=', '', post_url($url, $token_params));
}
You can call the method like so $token = get_app_access_token('APP_ID','SEKRET');
Upvotes: 5