上传图片至 Facebook

发布于 2024-09-05 08:52:43 字数 1742 浏览 4 评论 0原文

我正在尝试将图像上传到 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);

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

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

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

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.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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-09-12 08:52:43

好的,开始工作了,这里是代码,假设您正在进行有效的会话。

    $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);

这会将指定的图像上传到会话持有者图库,如果没有,它将创建一个相册,您也可以通过会话数组访问令牌,如上所示。
希望这可以帮助别人。

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.

梦幻的味道 2024-09-12 08:52:43

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

要通过 OAuth 请求权限,请使用
你的范围参数
授权请求,并包括
所有的逗号分隔列表
您想要请求的权限。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文