0

我的代码有这个问题,我正在尝试将图像上传到用户墙上,但它给了我以下错误:

致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。在第 1238 行扔进 /var/www/parkourjams.dk/emilaagaard/customers/fbgen/core/facebook/base_facebook.php

我的代码是:

<?php
session_start();
require 'facebook/facebook.php';
$app_id = "438648619527874";
$app_secret = "HIDDEN";
$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $app_secret,
  'fileUpload' => true,
  'cookie' => true
));

$post_login_url = "http://emilaagaard.dk/customers/fbgen/core/test.php";
$code = $_REQUEST["code"];
if (empty($code)) {
    $dialog_url= "http://www.facebook.com/dialog/oauth?"
       . "client_id=" .  $app_id 
       . "&redirect_uri=" . urlencode( $post_login_url)
       .  "&scope=publish_stream";
      echo("<script>top.location.href='" . $dialog_url 
      . "'</script>");
} else {
    $token_url="https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $app_id 
       . "&redirect_uri=" . urlencode( $post_login_url)
       . "&client_secret=" . $app_secret
       . "&code=" . $code;

       // curl
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, $token_url);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
       $response = curl_exec($ch); 
       curl_close($ch);

      $params = null;
      parse_str($response, $params);
      $access_token = $params['access_token'];
      echo "Token: " . $access_token . "<br /><br />";

    $graph_url= "https://graph.facebook.com/me/photos?"
        . "access_token=" .$access_token;

    // Post it!
    $user = $facebook->getUser();

    $args = array(
      'message' => 'Photo Caption', 
      'image' => '@'.realpath("ac.png")
    );

    $user = $facebook->getUser();
    $user_profile = $facebook->api('/me');

    if ($user) {
        print_r($user);
        echo "Username: " . $user_profile['username'];
        try {
            // We have a valid FB session, so we can use 'me'
            $data = $facebook->api('/me/photos', 'post', $args);
            echo $data;
        } catch (FacebookApiException $e) {
            print $e;
        }
    } else {
        echo "Dead user";
        $loginUrl = $facebook->getLoginUrl();
        print ', try <a href="' . $loginUrl . '">login</a></script>';
    }

    echo '</body></html>';
}
?>

我真的希望那里有人可以帮助我,我很累,并且可能因为在聚会结束后熬夜而对自己进行了代码盲化。

谢谢!

4

1 回答 1

1

如果你在本地服务器上测试它

插入

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);


插入

Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false;

$facebook->setAccessToken($access_token);

$user = $facebook->getUser();

于 2012-12-17T14:44:13.133 回答