5

我正在尝试将图像上传到 facebook 粉丝页面上的画廊,这是我到目前为止的代码,

$ch = curl_init();

        $data = array('type' => 'client_cred', 'client_id' => 'app_id','client_secret'=>'secret_key',' redirect_uri'=>'http://apps.facebook.com/my-application/'); // your connect url here

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        $rs = curl_exec($ch);
        curl_close($ch);

        $ch = curl_init();
        $data = explode("=",$rs);
        $token = $data[1];

        $album_id = '1234';
        $file= 'http://my.website.com/my-application/sketch.jpg';
        $data = array(basename($file) => "@".realpath($file),
        //filename, where $row['file_location'] is a file hosted on my server
            "caption" => "test",
            "aid" => $album_id, //valid aid, as demonstrated above
            "access_token" => $token
        );

        $ch2 = curl_init();
        $url = "https://graph.facebook.com/".$album_id."/photos";
        curl_setopt($ch2, CURLOPT_URL, $url);
        curl_setopt($ch2, CURLOPT_HEADER, false);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, false);
        curl_setopt($ch2, CURLOPT_POST, true);
        curl_setopt($ch2, CURLOPT_POSTFIELDS, $data);
        $op = curl_exec($ch2);

当我回显 $token 时,我似乎正在获取令牌,但是当我运行脚本时,我收到此错误,{"error":{"type":"OAuthAccessTokenException","message":"An access token is required请求此资源。"} ,我不知道为什么要这样做!

基本上我在该代码中所做的是使用 curl 获取访问令牌,然后也通过 curl 将照片上传到我的相册,但我一直收到该错误!

任何帮助将不胜感激,谢谢!

4

2 回答 2

4

Ok, got it working, here is the code, assuming that you have a valid session going.

    $token = $session['access_token'];

    //upload photo
    $file= 'photo.jpg';
    $args = array(
    'message' => 'Photo from application',
    );
    $args[basename($file)] = '@' . realpath($file);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/me/photos?access_token='.$token;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    $data = curl_exec($ch);

This will upload the specified image to the session holders gallery, it will create a album if there is not one present, also you can access the token via the session array as demonstrated above. Hope this helps someone out.

于 2010-06-28T07:21:14.573 回答
0

可能您的应用程序没有所需的权限。

要通过 OAuth 请求权限,请在您的授权请求中使用 scope 参数,并包含您要请求的所有权限的逗号分隔列表。

http://developers.facebook.com/docs/authentication/permissions

于 2010-06-11T13:33:16.717 回答