Facebook access_token问题 [英] Facebook access_token problem
本文介绍了Facebook access_token问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&redirect_uri=http:// www.facebook.com/pages/Cosmetics/167231286678063?sk=app_233227910028874&grant_type=client_credentials');
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ ch,CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,0);
$ token = curl_exec($ ch);
$ me = $ facebook-> api('/ me?'。$ token);
但是当我尝试在墙上发布时显示此错误:
致命错误:未捕获OAuthException:无效的OAuth访问令牌签名。
解决方案
为什么不使用PHP SDK 3.0.1?
如果您使用Facebook提供的sdk,它很容易,以下是使用php sdk 3.0.1进行身份验证的示例:
;?php
//需要Facebook PHP SDK 3.0.1:https://github.com/facebook/php-sdk/
require('facebook.php');
define('FACEBOOK_APP_ID',你的APP-ID在这里);
define('FACEBOOK_SECRET',你的APP-API-SECRET-HERE);
define('REDIRECT_URI',您的REDIRECT-URL-HERE);
define('PERMISSIONS_REQUIRED',publish_stream,user_photos);
$ user = null;
$ facebook = new Facebook(array(
'appId'=> FACEBOOK_APP_ID,
'secret'=> FACEBOOK_SECRET,
'cookie'=> true
));
$ user = $ facebook-> getUser(); //获取已连接用户的UID,如果Facebook用户未连接,则为0。
if($ user == 0){
//如果用户未连接到您的应用程序,请将用户重定向到身份验证页
/ **
*获取与重定向一起使用的登录URL。默认情况下,全页重定向是
*。如果您在
* JavaScript中使用带有window.open()调用的生成的URL,则可以在$ params中传入display = popup。
*
*参数:
* - redirect_uri:成功登录后要访问的URL
* - 范围:请求的扩展perms的逗号分隔列表
* /
$ login_url = $ facebook-> getLoginUrl($ params = array('redirect_uri'=> REDIRECT_URI,'scope'=> PERMISSIONS_REQUIRED));
echo(< script> top.location.href ='$ login_url。'< / script>);
} else {
//如果用户已经连接,然后提取access_token和用户信息或向登录用户显示一些内容。
try
{
$ access_token = $ facebook-> getAccessToken(); //给你当前用户的access_token
$ user = $ facebook-> api('/ me'); //根据用户授予您的应用程序的权限获取用户信息。
} catch(FacebookApiException $ e){
$ results = $ e-> getResult();
//如果要调试,请打印结果。
}
}
?>
I use a request dialog in my app where the user can send requests to their friends. Once the user sends the request i redirect the app to a page where it posts on the users page. I use the below code to get an access_token:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&redirect_uri=http://www.facebook.com/pages/Cosmetics/167231286678063?sk=app_233227910028874&grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
$token = curl_exec($ch);
$me = $facebook->api('/me?'.$token);
but when I try to post on the wall it shows this error:
Fatal error: Uncaught OAuthException: Invalid OAuth access token signature.
解决方案
Why don't you use PHP SDK 3.0.1 ?
Its easy if you use the sdk provided by facebook, here's the sample for authentication using php sdk 3.0.1:
<?php
// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');
define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
define('PERMISSIONS_REQUIRED', "publish_stream,user_photos");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.
if($user == 0) {
// If the user is not connected to your application, redirect the user to authentication page
/**
* Get a Login URL for use with redirects. By default, full page redirect is
* assumed. If you are using the generated URL with a window.open() call in
* JavaScript, you can pass in display=popup as part of the $params.
*
* The parameters:
* - redirect_uri: the url to go to after a successful login
* - scope: comma separated list of requested extended perms
*/
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));
echo ("<script> top.location.href='".$login_url."'</script>");
} else {
// if the user is already connected, then fetch access_token and user's information or show some content to logged in user.
try
{
$access_token = $facebook->getAccessToken(); // Gives you current user's access_token
$user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.
} catch(FacebookApiException $e){
$results = $e->getResult();
// Print results if you want to debug.
}
}
?>
这篇关于Facebook access_token问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文