我试着用curl通过curl来获取Facebook code和access_token 。 所以我可以在墙上张贴消息。 我知道sdk ,但如果我只使用容易curl方式?
一些故障返回:
Method Not Implemented
Invalid method in request
这是我的代码。 BY THE WAY:如何在1小时内记住access_token如果它仍然有效? 谢谢。
$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编辑:
$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;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 wallEDIT:
$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;最满意答案
尝试使用这些辅助方法,它将对图API进行cURL调用并返回应用程序access_token。
此函数接受一个url和一组参数,并通过cURL进行POST:
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; }此功能会在开发者应用中提供您的应用ID和密码,并为您的应用返回一个有效的access_token:
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)); }你可以这样调用方法$token = get_app_access_token('APP_ID','SEKRET');
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');
更多推荐
发布评论