【问题标题】:Read Facebook page post and token expiration阅读 Facebook 页面帖子和令牌到期
【发布时间】:2012-02-05 16:48:48
【问题描述】:

我需要从服务器读取特定粉丝页面的流。 我试图阅读图形 api https://graph.facebook.com//feed?access_token=&limit=100 它有效。 我需要了解令牌是否会过期以及如何以编程方式对其进行更新。 现在我通过http://developers.facebook.com/tools/explorer/ 应用程序生成我的令牌。 你能帮我么? 我正在使用 PHP sdk 谢谢, 一个。

【问题讨论】:

    标签: facebook facebook-graph-api facebook-php-sdk


    【解决方案1】:

    您可以使用以下代码阅读facebook页面,也可以获取指定字段

    https://graph.facebook.com/$page_id/?fields=link,etc&access_token=page_access_token
    

    $response = $fb->api($page_id .  '/?fields=link,etc&'. $access_token, 'GET')
    

    以下是四种场景的解决方案

    1.令牌在过期时间后过期(默认为2小时)。
    2.用户更改密码导致访问令牌无效。
    3.用户取消对您的应用的授权。
    4.用户退出Facebook。

    为确保为您的用户提供最佳体验,您的应用需要准备好捕捉上述场景的错误。以下 PHP 代码向您展示了如何处理这些错误并检索新的访问令牌。

    当您将用户重定向到身份验证对话框时,如果用户已经授权您的应用程序,则不会提示用户授予权限。 Facebook 将返回一个有效的访问令牌,而无需任何面向用户的对话。但是,如果用户取消了对您的应用程序的授权,那么用户将需要重新授权您的应用程序才能让您获得 access_token。

    <?php
    $app_id = "YOUR_APP_ID";
    $app_secret = "YOUR_APP_SECRET"; 
    $my_url = "YOUR_POST_LOGIN_URL";
    
    // known valid access token stored in a database 
    $access_token = "YOUR_STORED_ACCESS_TOKEN";
    
    $code = $_REQUEST["code"];
    
    // If we get a code, it means that we have re-authed the user 
    //and can get a valid access_token. 
    if (isset($code)) {
    $token_url="https://graph.facebook.com/oauth/access_token?client_id="
      . $app_id . "&redirect_uri=" . urlencode($my_url) 
      . "&client_secret=" . $app_secret 
      . "&code=" . $code . "&display=popup";
    $response = file_get_contents($token_url);
    $params = null;
    parse_str($response, $params);
    $access_token = $params['access_token'];
    }
    
    
    // Attempt to query the graph:
    $graph_url = "https://graph.facebook.com/me?"
    . "access_token=" . $access_token;
    $response = curl_get_file_contents($graph_url);
    $decoded_response = json_decode($response);
    
    //Check for errors 
    if ($decoded_response->error) {
    // check to see if this is an oAuth error:
    if ($decoded_response->error->type== "OAuthException") {
      // Retrieving a valid access token. 
      $dialog_url= "https://www.facebook.com/dialog/oauth?"
        . "client_id=" . $app_id 
        . "&redirect_uri=" . urlencode($my_url);
      echo("<script> top.location.href='" . $dialog_url 
      . "'</script>");
    }
    else {
      echo "other error has happened";
    }
    } 
    else {
    // success
    echo("success" . $decoded_response->name);
    echo($access_token);
    }
    
    // note this wrapper function exists in order to circumvent PHP’s 
    //strict obeying of HTTP error codes.  In this case, Facebook 
    //returns error code 400 which PHP obeys and wipes out 
    //the response.
    function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
    }
    ?>
    

    有关更多详细信息,您可以访问此link
    谢谢

    【讨论】:

    • 非常感谢。此代码非常适合基于用户的阅读。如果可能的话,我需要一个仅在服务器上工作而无需用户交互的系统。有可能吗?
    • @Andrea 但这违反了 Facebook 条款
    • 对不起,我不明白为什么。你能指出在这种情况下适用的规则吗?非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多