Facebook上传照片到页面

时间:2016-05-14 08:18:09

标签: javascript php android facebook facebook-graph-api

我管理我的Facebook页面,我想将照片上传到我的页面。我获得了Long long令牌并且还获得了manage_page的应用程序许可。我已下载SDK并将其添加到php页面。我现在有以下的事情该做什么?

<?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 ? 

此时我非常困惑。 :/ 任何帮助,将不胜感激。 :)

谢谢!

1 个答案:

答案 0 :(得分:0)

这是Javascipt,但我认为它可能很有用,如果你想将你的照片发布到特定的专辑,user_photos允许你。

试试这个:

 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'
         });
     }
 });};

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");
                }
            }
        );
    }
);};

另外,要获取您的Facebook页面ID,您应该执行以下操作:

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'});

如果需要更多帮助评论

此致