I need to share an image to facebook. For this I am trying the API call suggested here
FB.login(function(response) {
if (response.authResponse) {
FB.api(
"/{page-id}/photos",
"POST",
{
"url": "https:\/\/www.facebook.com\/images\/fb_icon_325x325.png",
"published": "false"
},
function (response) {
if (response && !response.error) {
console.log(JSON.stringify(response))
/* handle the result */
}
else
{
console.log("ERROR: " + JSON.stringify(response))
}
}
);
} else {
console.log('User cancelled login or did not fully authorize.');
}
});
But I am getting an error (#803) Some of the aliases you requested do not exist: {page-id}
Is page-id the same as the userID returned in the authResponse? Because I also tried hardcoding that id, but I got a deprecated API message.
I need to allow the user to share the photo directly to his feed.
Thanks
Page ID and User ID are not the same. You get a User ID after authorization, you get a Page ID by using the /me/accounts endpoint with a User Token. You can also get Page Tokens with /me/accounts?fields=access_token. You need to use a Page Token with the publish_pages and manage_pages permissions to create posts or photos on a Page you manage.
As WizKid commented, you can also just use /me/photos to upload photos. Just use a Page Token.
More information: https://developers.facebook.com/docs/facebook-login/access-tokens/
Related
I make a request to find out the data about my user access token:
fetch("https://graph.facebook.com/v8.0/debug_token?input_token=" + access_token).then(function (response) {
response.text().then(function (textII) {
alert(textII);
});
});
In this case, I take the accessToken value from auth Response after authorization to the application and Facebook:
{
status: 'connected',
authResponse: {
accessToken: '{access-token}',
expiresIn:'{unix-timestamp}',
reauthorize_required_in:'{seconds-until-token-expires}',
signedRequest:'{signed-parameter}',
userID:'{user-id}'
}
}
But I get an error:
{
"error": {
"message": "(#100) You must provide an app access token or a user access token that is an owner or developer of the app",
"type": "OAuthException",
"code": 100,
"fbtrace_id": "AtpM_XaTOgH7YH-OXHdQCXj"
}
}
The value of my variable access_token is not null (I checked), while this is sort of a user access token (the keyword "sort of"). So what's wrong? Explain, please. If you need information about what I need it for, then I need it for this request:
for (var iio = 0; iio < posts_ids.length; iio++) {
fetch("https://graph.instagram.com/" + posts_ids[iio] + "?fields=id,media_type,media_url,owner,timestamp&access_token=" + access_token).then(function (response) {
response.text().then(function (textII) {
alert(textII);
});
});
}
Which throws an error:
{
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190,
"fbtrace_id": "AD7BAPqUGdQGC6dyV9K_FFr"
}
}
At the same time, in my previous requests, just starting with "https://graph.facebook.com/", and not "https://graph.instagram.com/", everything worked with the accessToken that I received after registration.
Help me please. I'm new to the Instagram API, so I can be pretty dumb.
Please turn off the setting(Setting -> Advanced -> Application authentication), it works!
#Denis Bredun , you would have to pass access_token field also as the URL param, so just update the url to -
https://graph.facebook.com/v8.0/debug_token?input_token=<access_token>&access_token=<access_token>
Try resetting your app secret. I was getting the same problem, and this fixed it.
I solved the error by changing the app secret to the correct one.
Make sure you are using the proper app secret.
For me, using the debug_token api requires the "page token" and the "app token" and then you call the api in the format :
https://graph.facebook.com/v<version>/debug_token?input_token=<page_token>&access_token=<app_token>
As per the facebook doc To get the app_token, you need the app id and app secret. And then you call https://graph.facebook.com/oauth/access_token?client_id={your-app-id}&client_secret={your-app-secret}&grant_type=client_credentials
To get the page_token, this is done by first getting the user_token, then you convert the user_token into a long lived user token, and then you get the page token calling the api "/<page_id>?fields=access_token".
Note that you will need the proper permissions on the user_token to do that, like "pages_read_engagement" otherwize you will get an error from the api trying to get the page token.
I am trying to fetch the information about my friends who are also logged in (or using) the app using facebook , for that i am using graph api . But i am not getting relevant information . Can anyone please help me out on that .
I went through the facebook developer docs and tried many option but it didn't helped .
//Request
FB.api(
"/{app-id}", // app-id for the application
{
"fields": "context.fields(friends_using_app)"
},
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
Using this when i am trying to fetch the information i am getting output like this
Not getting the name and other information of my friend who is also using the same app using facebook login .
This would be the correct code to get all friends who authorized the App too:
FB.api('/me/friends', (response) => {
if (response && !response.error) {
/* handle the result */
}
}
I am trying to get the users liked Facebook pages, here is my code:
exports.checkUserInterests = function (fbaccountID,fbModule) {
var facebookModule = fbModule;
;
//code to get the list of facebook likes for any given user
facebookModule.requestWithGraphPath('me/interests', {}, 'GET', function(e) {
if (e.success) {
alert(e.result);
} else if (e.error) {
alert(e.error);
} else {
alert('Unknown response');
}
});
};
My Permissions is set as following:
//set permissions for graph api
var permissions = ['user_friends', 'user_interests', 'user_birthday', 'user_photos', 'basic_info'];
I an returning an empty json array, is my request correct?
Thanks
I use a different call to get the Facebook Likes of a user, maybe thats the problem.
For example, if I want to know if the user likes a page with id: xxxxx, I do a GET call to "me/likes/xxxxxx" and see if the result is true.
For this to work, you should have the permissions to user_likes.
I am trying to build a comments method within a website without the Facebook comments plugin.
It's a photogallery portion of a website and all the photos are actually hosted on the business Page, I'm doing this to make use of FBs uploading, resizing/rotate etc rather than having to build that myself too.
As a happy bi-product to this I figured I could link comments on the website with comments on FB and have a syncrhonised comments system whereby comments posted on either site (my website or FB) will show equally on either site.
So, I have worked out how to post the comments, but it is always posting as the Business Page, not the logged in user.
This code posts the comment, the variable obj is the ID of the photo on Facebook. msg is obviously the comment text. I have setup methods to get the correct permissions where required which are called if the response from the post states that this is needed.
FB.api('/' + obj + '/comments',
'post',
{ "message": msg,
"access_token": accessToken,
"from": uid
}, // do the post
function (response) { // check the response
if (!response || response.error) { // if there's an error
// DO STUFF WITH ERRORS/REQUEST PERMS ETC
} else {
alert("Msg posted id: " + response.id);
};
});
Everything above works, but when the comment appears, it is always showing from: my Page ID not, what I need which would be from: my user id
accessToken and UID is being retrieved as follows:
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
uid = response.authResponse.userID;
accessToken = response.authResponse.accessToken;
}
});
The para from in the first function there posts the User ID, NOT the page ID, but whether that para is there or not, it always posts the comment as the Page.
It's frustrating the hell out of me as so far this has been pretty simple to achieve!
I am just getting acquainted with Facebook javascript SDK.
What is the proper way to check if a permission request was successful then call a function? In my case the user is being requested additional permissions. They have already given basic permissions.
Right now I have this.. but it fires the function regardless of success or not
FB.login(function (publishAuth) {
if (publishAuth.authResponse) {
authSuccess();
} else { }
}, { scope: 'publish_stream' });
}
Thanks for the help
Mark
What is the proper way to check if a permission request was successful then call a function?
Since the user can accept/deny each (extended) permission individually, there is no simple true/false response to a permissions request.
To check, which permissions the user has actually granted and which not, you have to make a call to /me/permissions – see Graph API Explorer example.
(With field expansion you can do it also in one call while requesting other infos about the user – f.e. /me?fields=id,name,permissions)
You will always have an authResponse, but that doesn't mean you have the permissions you need, for that you need to check the properties IN the authResponse.
From Facebooks developers page:
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});