获取访问令牌

发布于 2024-11-11 09:31:17 字数 802 浏览 4 评论 0原文

我尝试使用下面的代码访问访问令牌:

$code=$_REQUEST['code'];

if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&scope=publish_stream,user_photo_video_tags";
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

它工作正常,但是当用户第二次尝试使用该应用程序时,在尝试授权用户时没有流畅的流程。

所以,我的问题是如何确保在尝试授权用户时流程顺畅?

I have tried accessing the access token using the code below:

$code=$_REQUEST['code'];

if(empty($code)) {
    $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=" 
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&scope=publish_stream,user_photo_video_tags";
    echo("<script>top.location.href='" . $dialog_url . "'</script>");
  }

  $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
    . $app_id . "&redirect_uri=" . urlencode($my_url) 
    . "&client_secret=" . $app_secret 
    . "&code=" . $code;
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];

It works fine but when the user tries to use the application for the second time, there is no smooth flow while trying to authorize the user.

So, my question is how do I make sure that there smooth flow while trying to authorize the users?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花伊自在美 2024-11-18 09:31:17

为了顺利进行用户授权,我建议您使用最新的 Facebook PHP SDK

这是我的一个示例脚本它处理用户授权:

// init new facebook class instance with app info
$facebook = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET'
));

// get user UID
$fb_user_id = $facebook->getUser();

// get the url where to redirect the user
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream'));

// check if we have valid user
if ($fb_user_id) {
try {
    // Proceed knowing you have a logged in user who's authenticated.
    $fb_user_profile = $facebook->api('/me');   

} catch (FacebookApiException $e) {
    $fb_user_id = NULL;
    // seems we don't have enough permissions
    // we use javascript to redirect user instead of header() due to Facebook bug
    print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

    // kill the code so nothing else will happen before user gives us permissions
    die();
}

} else {
// seems our user hasn't logged in, redirect him to a FB login page

print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

// kill the code so nothing else will happen before user gives us permissions
die();
}

// at this point we have an logged in user who has given permissions to our APP
// basic user info can be fetched easily
print "User access token is: ". $facebook->getAccessToken();

To do user authorization smoothly I recommend you to use the latest Facebook PHP SDK

Here's an example script of mine which handles the user authorization:

// init new facebook class instance with app info
$facebook = new Facebook(array(
'appId' => 'YOUR APP ID',
'secret' => 'YOUR APP SECRET'
));

// get user UID
$fb_user_id = $facebook->getUser();

// get the url where to redirect the user
$location = "". $facebook->getLoginUrl(array('scope' => 'publish_stream'));

// check if we have valid user
if ($fb_user_id) {
try {
    // Proceed knowing you have a logged in user who's authenticated.
    $fb_user_profile = $facebook->api('/me');   

} catch (FacebookApiException $e) {
    $fb_user_id = NULL;
    // seems we don't have enough permissions
    // we use javascript to redirect user instead of header() due to Facebook bug
    print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

    // kill the code so nothing else will happen before user gives us permissions
    die();
}

} else {
// seems our user hasn't logged in, redirect him to a FB login page

print '<script language="javascript" type="text/javascript"> top.location.href="'. $location .'"; </script>';

// kill the code so nothing else will happen before user gives us permissions
die();
}

// at this point we have an logged in user who has given permissions to our APP
// basic user info can be fetched easily
print "User access token is: ". $facebook->getAccessToken();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文