开发者

Uploading a picture to facebook

I am trying to upload a image to a gallery on a facebook fan page, here is my code thus far,

$ch = curl_init();

        $data = array('type' => 'client_cred', 'client_id' => 'app_id','client_secret'=>'secret_key',' redirect_uri'=>'http://apps.facebook开发者_如何学Python.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);

When I echo $token, I seem to be getting the token, but when I run the script, I get this error, {"error":{"type":"OAuthAccessTokenException","message":"An access token is required to request this resource."} , I have NO idea why it is doing this!

Basically what I am doing in that code is getting the access token with curl, and then, uploading the photo to my album also via curl, but I keep getting that error!

Any help would be appreciated, thank you!


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.


Probably your application doesn't have needed permissions.

To request permissions via OAuth, use the scope argument in your authorization request, and include a comma separated list of all the permissions you want to request.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜