There are many posts about "/me" returns undefined, but I couldn't find any solution.
The user is logged in, because response.status is connected. But calling /me returns "undefined".
Code snippet:
if (response.status === 'connected') {
// The response object is returned with a status field that lets the app know the current
// login status of the person. In this case, we're handling the situation where they
// have logged in to the app.
FB.api('/me', function(info) {
alert("facebook:"+info.id);
});
Related
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/
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!
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;
alert(uid);
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
// logged in and connected user, someone you know
var uid = response.id;
alert(uid);
} else {
// the user isn't logged in to Facebook.
alert("the user isn't logged in to Facebook.");
}
});
How to get uid when not_authorized ?
How to get uid when not_authorized ?
Not at all. Facebook does not give you such information, unless the user has authorized your app.
Itβs a matter of privacy β think about it, every site on the net could set up their own app, and would get your FB user ID once you browse it. Would you want f.e. a random p0rn site that you might get redirected to from somewhere else without you even wanting this to happen to get your Facebook user ID? Of course you would not.
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.
}
});
I'm using PhoneGap/Cordova with the facebook plugin. Everything seems to work except for getLoginStatus who is not working as defined here and here. When called, it returns some data, but not all: it doesn't return userID nor signedRequest.
Here is the code:
FB.getLoginStatus(function(response) {
if (response.status == 'connected') {
var fb_uid = response.authResponse.userID;
var fb_signedRequest = response.authResponse.signedRequest;
alert('logged in');
} else {
alert('not logged in');
}
});
userID is filled with ellipsis (...), while signedRequest is undefined.
I managed to get userID with a graph call to /me:
FB.api('/me', function(me){
if (me.id) {
var fb_uid = me.id;
}
});
I wasn't able to find any way in the documentation to get a signed_request, which I have to use to authenticate the facebook user to a remote service to whom the user already connected to with facebook (I already made a login call so user is OK).
Basically the problem is that my call to getLoginStatus returns
{
status: 'connected',
authResponse: {
session_key: true,
accessToken: 'a long string...',
expiresIn:'a number',
sig: '...', //exactly this string
userID:'...' //exactly this string
secret:'...' //exactly this string
expirationTime:'a long number'
}
}
instead of what documented
As a background, when authentication happens using the plugin then the JavaScript SDK API calls the iOS/Android SDK to handle the authorization and then pass response auth data back to the JS part. The native (iOS/Android) SDKs do not get signed requests back to be able to pass this on to the JS. This is why it's empty.
If you use the latest plugin you should at least now be seeing the user ID. The one from June likely did not pass this back. Otherwise as a work around, you could perform a call to the /me endpoint when authentication is successful in your JS code to get the user id. See the Hackbook example that does this.