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