OAuth(Facebook 应用程序)出现身份验证问题,会话不可用?

发布于 2024-10-17 23:44:05 字数 2584 浏览 1 评论 0 原文

我想读取当前用户朋友的所有生日。我使用 facebook 的新 Graph API。我请求基于 Facebook read_friendslist 和 friends_birthday)授权rel="nofollow">见解示例php-sdk 示例。为了读取好友列表和用户详细信息,我使用了 Graph API 和 Facebook PHP SDK。

接下来的代码片段是我的方法的一个简短的、独立的正确示例。如果我尝试使用我的应用程序,它会请求登录,然后请求权限,然后由于没有可用的会话而无法打印我的所有朋友。这是怎么回事?

首先是下面的index.php使用的birthday.php,我删除了一些样板代码或我认为它不会导致此问题的代码(由[.. .])。您可以在这个问题的末尾找到完整的代码。

<?php
function get_birthday_of_friends() {

    $fbconfig['appid' ]  = "MY_APP_ID";
    $fbconfig['secret']  = "MY_APP_SECRET";

    try{
            include_once "facebook/src/facebook.php";
    }
    catch(Exception $o){
             // [...] log error
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    $session = $facebook->getSession();

    $fbme = null;
    // Session based graph API call.
    if ($session) {
               // [...] return birthdays
    } else {
        echo "No session found";
    }
}
?>

所需的 lib.php 与见解示例相同。

<?php

    // [...] Include and define app-id etc.

function get_access_token($base_url) {
  if (isset($_REQUEST['access_token'])) {
    return $_REQUEST['access_token'];
  }
  $params = array();
  $params['client_id'] = APP_ID;
  $params['redirect_uri'] = $base_url;
  if (!isset($_REQUEST['code'])) {
    $params['scope'] = 'read_friendlists, friends_birthday';
    $url = FacebookMethods::getGraphApiUrl('oauth/authorize', $params);
    throw new RedirectionException($url);
  } else {
    $params['client_secret'] = APP_SECRET;
    $params['code'] = $_REQUEST['code'];
    $url = FacebookMethods::getGraphApiUrl('oauth/access_token');
    $response = FacebookMethods::fetchUrl($url, $params);
    $response = strstr($response, 'access_token=');
    $result = substr($response, 13);
    $pos = strpos($result, '&');
    if ($pos !== false) {
      $result = substr($result, 0, $pos);
    }
    return $result;
    }
}

    // [...] Call get_access_token() and get_birthday_of_friends()!
?>

你能帮我吗?我在pastebin.com上添加了整个源代码,如果这可以帮助您识别我的问题。 Pastebin.com 上的“index.php”和“birthday.php"。

先感谢您!

I want to read all birthdays of the friends from current user. I use the new Graph API of facebook. I request the authorization of the permissions (read_friendslist and friends_birthday) based on Facebooks insights example and php-sdk example. For reading the friendslist and the user details I used the Graph API with Facebook PHP SDK.

The upcoming code snippets are a short self contained correct example of my approach. If I try to use my app it requests login, then asks for permissions and then fails in printing all my friends due to the fact that no session is available. What's wrong here?

First is the birthday.php which is used by the following index.php, I removed some boilerplate code or code I think it's not causing this problem (identified by [...]). You can find the complete code on the end of this question.

<?php
function get_birthday_of_friends() {

    $fbconfig['appid' ]  = "MY_APP_ID";
    $fbconfig['secret']  = "MY_APP_SECRET";

    try{
            include_once "facebook/src/facebook.php";
    }
    catch(Exception $o){
             // [...] log error
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    $session = $facebook->getSession();

    $fbme = null;
    // Session based graph API call.
    if ($session) {
               // [...] return birthdays
    } else {
        echo "No session found";
    }
}
?>

The required lib.php is identically with the insights example.

<?php

    // [...] Include and define app-id etc.

function get_access_token($base_url) {
  if (isset($_REQUEST['access_token'])) {
    return $_REQUEST['access_token'];
  }
  $params = array();
  $params['client_id'] = APP_ID;
  $params['redirect_uri'] = $base_url;
  if (!isset($_REQUEST['code'])) {
    $params['scope'] = 'read_friendlists, friends_birthday';
    $url = FacebookMethods::getGraphApiUrl('oauth/authorize', $params);
    throw new RedirectionException($url);
  } else {
    $params['client_secret'] = APP_SECRET;
    $params['code'] = $_REQUEST['code'];
    $url = FacebookMethods::getGraphApiUrl('oauth/access_token');
    $response = FacebookMethods::fetchUrl($url, $params);
    $response = strstr($response, 'access_token=');
    $result = substr($response, 13);
    $pos = strpos($result, '&');
    if ($pos !== false) {
      $result = substr($result, 0, $pos);
    }
    return $result;
    }
}

    // [...] Call get_access_token() and get_birthday_of_friends()!
?>

Can you help me with that? I added the whole source code on pastebin.com if this helps you to identify my problem. Source code on pastebin.com for "index.php" and "birthday.php".

Thank you in advance!

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

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

发布评论

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

评论(3

回眸一笑 2024-10-24 23:44:05

我不确定您使用的方法是否已弃用,但我知道这是旧方法,您应该尝试使用新方法以获得身份验证令牌。

看看这个链接:
http://developers.facebook.com/docs/authentication/signed_request/

在乍一看,您必须:

  • 从 $_REQUEST 获取 signed_request 参数。
  • 使用中提供的示例函数
    解码后的链接
    它,你将有一个数组,其中
    有一个参数叫
    oauth_token
  • 通过这个参数就可以启动
    通过以下方式调用图表
    将其附加到 URL 例如
    *https://graph.facebook.com/PROFILE_ID/pictures/?access_token=OAUTH_TOKEN*

确保您在应用程序的配置设置(“高级”选项卡)中启用了 Oauth 2.0 for Canvas

I am not sure if the method that you are using is deprecated or not, but I know it's the old way and you should try with the new one in order to get the auth token.

Take a look at this link:
http://developers.facebook.com/docs/authentication/signed_request/

In a glance, you have to:

  • Get the signed_request parameter from $_REQUEST.
  • Use the sample function provided in
    the link to decode it Once you decode
    it, you will have an array in which
    there is a parameter called
    oauth_token.
  • With this parameter, you can start
    making calls to the Graph by
    appending it to the URL e.g.
    *https://graph.facebook.com/PROFILE_ID/pictures/?access_token=OAUTH_TOKEN*

Make sure that you have Oauth 2.0 for Canvas enabled into the Configuration settings of your app (Advanced tab).

玩物 2024-10-24 23:44:05

我认为在某些浏览器中第三方 cookie 存在问题。你在 Safari 中测试吗?另外,尝试向 loginUrl 添加权限 - 这比使用 oauth 添加和请求权限更简单。

I think in some browsers there's a prblem with third party cookies. Are you testing in Safari? And also, try to add permissions to the loginUrl - it's a bit more simple than adding and requesting the permissions with oauth.

空气里的味道 2024-10-24 23:44:05

如果没有可用的会话,我必须重定向到登录页面并需要带有参数的扩展权限。这对我很有帮助,感谢 manuelpedrera 的帮助。

$facebook->getLoginUrl(array('req_perms' => 'read_friendlists, [...]'));

If no session is available, I had to redirect to the login page and require the extended permissions with the parameters. This did the trick to me, thanks to manuelpedrera for helping me out.

$facebook->getLoginUrl(array('req_perms' => 'read_friendlists, [...]'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文