gpt4 book ai didi

php - 验证 facebook canvas 应用程序返回 ?code=

转载 作者:行者123 更新时间:2023-12-04 17:07:03 25 4
gpt4 key购买 nike

我的 facebook canvas 应用有问题,目前正在开发中 http://localhost:8080

我的 Canvas url 是 http://localhost:8080/fbcanvas/

在 facebook 上,url 设置为 http://apps.facebook.com/app_name/

问题是在用户批准我的应用程序后,我得到一个代码作为 $_GET['code'] 变量。在 facebook 文档中,它没有说任何有关获取 $_GET['code'] 的信息,它只是说获取 signed_request

这是我在 facebook 示例中使用的代码。

    require_once($_SERVER['DOCUMENT_ROOT'] . '/classes/Page.php');
require($_SERVER['DOCUMENT_ROOT'] . '/core/config.fb.php');

$canvas_page = 'http://apps.facebook.com/khawamusic/';
$auth_url = 'https://www.facebook.com/dialog/oauth?client_id=' . $app_id . '&redirect_uri=' . urlencode($canvas_page);
$signed_request = $_REQUEST['signed_request'];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

if(empty($data['user_id'])) {
echo('<script> top.location.href="' . $auth_url .'";</script>');
} else {

$page = new Page;

$styles = array('reset.css', 'fbcanvas.css');
$scripts = array(
'https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js',
'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js',
'http://connect.facebook.net/en_US/all.js#xfbml=1',
'/sources/js/fbcanvas.js'
);


$page->set_title('Khawa');
$page->set_styles($styles);
$page->set_scripts($scripts);
$page->start_page();
require($_SERVER['DOCUMENT_ROOT'] . '/fbcanvas/fb.tpl');
$page->end_page();

}

所以发生的事情是用户批准我的应用程序,然后他被重定向到 http://apps.facebook.com/khawamusic/?code=blabla

我很困惑,因为在文档中它没有说我应该得到一个 $_GET['code']

最佳答案

如果用户按下“允许”,则您的应用已获得授权。 OAuth 对话框会将用户的浏览器重定向(通过 HTTP 302)到您在 redirect_uri 参数中使用授权代码传递的 URL:

http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

有了这段代码,您可以继续下一步,即应用身份验证,以获得进行 API 调用所需的访问 token 。

引用:https://developers.facebook.com/docs/authentication/

编辑:这是身份验证的示例,这不会显示 ?code=Blabla..首先从这里下载最新的 Facebook PHP SDK: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”替换为来自 Facebook 的应用程序 ID 和 App Secret,我已经添加了使用图形 API 的样本墙发布,如果你不想,你可以删除它,如果你通过我的代码和评论,你会明白它的作用,你不想做任何事情来获取访问 token ,只需使用 $access_token 变量,它会给你当前用户的 access_token 如果你想要用户的 ID 那么使用 $user 变量,如果你想要用户的基本信息,使用 $userInfo 变量,它会使用 graph api 获取用户的数据并返回数组中的所有信息,你将获得当前用户的信息,如 id、name、first_name、last_name ,link,hometown,location,bio,work,education,gender,timezone.etc.

将 $RedirectUrl 更改为您的着陆页 URL 或 Canvas 页面 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);
}

}
?>

关于php - 验证 facebook canvas 应用程序返回 ?code=,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6642494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com