doufu6196 2016-05-14 08:18
浏览 87

Facebook上传照片到页面

I manage my facebook page and I want to upload photo to my page. I got Long lived token and also gave my app permission of manage_page. I have downloaded the SDK and added it on php page. I have following thing now what to do ?

<?php
session_start();

require_once 'facebook-php-sdk-v4-5.0.0/src/Facebook/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'my-app-id',
  'app_secret' => 'my-app-secret',
  'default_graph_version' => 'v2.5',
]);

$accessToken = "Here long lived token generated from graph API";

$response = $fb->post(); //which method should I use ? 

I'm very confused at this point. :/ Any help would be appreciated. :)

Thank you!

  • 写回答

1条回答 默认 最新

  • dousiyou1058 2016-05-23 20:43
    关注

    This is Javascipt but I think it could be useful, the permission user_photos allows you if you want to publish your photo to a specific album.

    Try this:

     function loginFacebook() {
     FB.getLoginStatus(function(response) {
         if (response.status === 'connected') {
             // the user is logged in and has authenticated the app,
             var uid = response.authResponse.userID;
             var accessToken = response.authResponse.accessToken;
             PostToFB(uid, accessToken);
         } else if (response.status === 'not_authorized') {
             // the user is logged in to Facebook,
             // but has not authenticated the app
             FB.login(function(response) {
                 current_login_status = response.status;
                 if (response.authResponse) {
                     var uid = response.authResponse.userID;
                     var accessToken = response.authResponse.accessToken;
                     PostToFB(uid, accessToken);
                 } else {
                     alert('User did not fully authorize.');
                 }
             }, {
                 scope: 'public_profile,publish_actions,publish_pages,manage_pages,user_photos'
             });
         } else {
             // the user isn't logged in to Facebook.;
             FB.login(function(response) {
                 current_login_status = response.status;
                 if (response.authResponse) {
                     var uid = response.authResponse.userID;
                     var accessToken = response.authResponse.accessToken;
                     PostToFB(uid, accessToken);
                 } else {
                     alert('User cancelled login or did not fully authorize.');
                 }
             }, {
                 scope: 'public_profile,publish_actions,publish_pages,manage_pages,user_photos'
             });
         }
     });};
    

    And PostToFB:

    function PostToFB(uid, accessToken) {
    // Do something with the response
    var page_id = "YOUR ID PAGE";
    FB.api(
        '/' + uid + '/accounts',
        function(returnData) {
            var page_accessToken = "";
            //Search on facebook pages from logged user
            for (var x = 0; x < returnData.data.length; x++) {
                if (page_id == returnData.data[x].id) {
                    page_accessToken = returnData.data[x].access_token;
                }
            }
            var linkImage = "YOUR LINK";
            var datasend = {
                access_token: page_accessToken,
                message: "MY MESSAGE",
                url: linkImage
            };
            FB.api(
                '/' + page_id + '/photos',
                'POST',
                datasend,
                function(response) {
                    if (response && !response.error) {
                        var postId = response.id;
                        alert("Thank you");
                    } else {
                        alert("Error");
                    }
                }
            );
        }
    );};
    

    Also to get your facebook page ID, you should do something like this:

    FB.login(function(response) {
    FB.api('/me/accounts', function(apiresponse) {
        var data = apiresponse['data'];
        var ids = new Array();
        for (var i = 0; i < data.length; i++) {
            console.log(data[i].id);
        }
    });}, {scope: 'manage_pages'});
    

    If need more help comment

    Regards

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记