如何处理过期访问令牌facebook PHP sdk

时间:2013-07-26 21:14:28

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

我在这里阅读了关于如何处理过期访问令牌的facbeook博客文章:https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/

但有些事情对我来说并不清楚,这让我很难取得进步。我列出了博客中的示例代码和我自己的php代码。我最想知道我需要对我的代码做出哪些更改,以便博客中的代码可以在我自己的代码中运行。我也对博客中的特定代码有一些疑问。对于初学者来说,$ _REQUEST [“code”]来自哪里?

如果您查看我的代码,您会看到我正在尝试向用户发送通知,我会事先得到应用访问令牌,然后进行api调用以及随之发送访问令牌;但是在示例代码中,使用curl_file_get_contents()而不是$ facebook-> api进行图形调用,我该怎么办? $ facebook-> api是否会返回与curl_file_get_contents()相同的响应?因为示例代码检查该错误的响应并重定向(如果有)。

//PHP SAMPLE CODE FROM THE BLOG
<?php
  $app_id = "YOUR_APP_ID";
  $app_secret = "YOUR_APP_SECRET"; 

//IS THIS SUPPOSED TO BE MY CANVAS APP URL? 
  $my_url = "YOUR_POST_LOGIN_URL";

  // known valid access token stored in a database 
  $access_token = "YOUR_STORED_ACCESS_TOKEN";

//WHERE IS THIS $CODE VARIABLE COMING FROM?

  $code = $_REQUEST["code"];


  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_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
  }
?>

//MY PHP 
    $token_url ="https://graph.facebook.com/oauth/access_token?" .
                "client_id=" . $APP_ID .
                "&client_secret=" . $APP_SECRET .
                "&grant_type=client_credentials";

$app_token = file_get_contents($token_url);
$app_token = str_replace("access_token=", "", $app_token);




$data = array(
    'href'=> 'https://apps.facebook.com/thebringernetwork/',
    'access_token'=> $app_token,
    'template'=> 'test'
);

// HOW DO I USE THAT ERROR CHECKING CODE AT THIS POINT? 
$facebook->api('/16xxxxx3/notifications', 'post', $data);

0 个答案:

没有答案