如果用户按“允许”,则您的应用程序已获得授权。 OAuth 对话框将(通过 HTTP 302)将用户的浏览器重定向到您在redirect_uri 参数中传递的带有授权代码的 URL:
http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER
有了此代码,您就可以继续下一步,即应用程序身份验证,以获取进行 API 调用所需的访问令牌。
Refer: https://developers.facebook.com/docs/authentication/ https://developers.facebook.com/docs/authentication/
EDIT:这是身份验证的示例,这不会显示 ?code=Blabla..
首先从这里下载最新的 Facebook PHP SDK:https://github.com/facebook/php-sdk/tree/master/src https://github.com/facebook/php-sdk/tree/master/src
确保保存所有 3 个文件:facebook.php、base_facebook.php 和 fb_ca_chain_bundle.crt
现在将文本“YOUR_APP_ID”和““YOUR_APP_API_SECRET”替换为您的应用程序 ID 和来自 facebook 的应用程序密钥,我已经使用图形 api 添加了示例墙发布,如果您不想要,您可以将其删除,如果您通过我的代码和注释,你会明白它的作用,你不想做任何事情来获取访问令牌,只需使用 $access_token 变量,它会给你当前用户的 access_token ,如果你想要用户的 ID 那么使用 $user 变量,如果您想要用户的基本信息,请使用 $userInfo 变量,它将使用图形 api 获取用户的数据并返回数组中的所有信息,您将获得当前用户的信息,例如 id,name,first_name,last_name ,链接,家乡,地点,个人简介,工作,教育,性别,时区等。
将 $RedirectUrl 更改为您的登陆页面 URL 或画布页面 URL
<?php
require 'facebook.php';
define('FACEBOOK_APP_ID', "YOUR_APP_ID"); // Your App ID
define('FACEBOOK_SECRET', "YOUR_APP_API_SECRET"); // Your App API Secret
$RedirectUrl = "http://apps.facebook.com/myapp/"; // Your Landing Page URL, User's will be redirect to this URL after they allow your app.
function d($d){
echo "<pre>";
print_r($d);
echo "</pre>";
}
$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(isset($_GET['code'])){
header("Location: $RedirectUrl");
}
if($user == 0) {
// If User is not connected to your app, then redirect 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('scope' => "publish_stream", 'redirect_uri' => $RedirectUrl));
echo("<script> top.location.href='" . $login_url . "'</script>");
} else {
// If User is connected to your app, then do something.
$signed_request = $facebook->getSignedRequest(); // Get the data from a signed_request token.
$access_token = $facebook->getAccessToken(); // Determines the access token that should be used for API calls.
$userInfo = $facebook->api("/me"); // Get's User Info
try {
// Posts to user's wall after the user allows your app.
$wallpost = array(
'message' => "I like this",
'link' => 'http://google.com',
'picture' => 'http://i.imgur.com/8iz6L.png',
'name' => 'This is cool',
'description'=> 'Checkout this cool app'
);
$publishStream = $facebook->api("/$user/feed", "post", $wallpost); // WallPost to User's Wall using Graph API
echo "Your post was successfully posted to UID: $user";
}
catch (FacebookApiException $e) {
d($e);
}
}
?>