2

我在这里阅读了有关如何处理过期访问令牌的 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);
4

0 回答 0