How to get first and last name without access token? - javascript

According to the FB SDK docs, it appears that some data from the response object I should be able to retrieve sans access token. (https://developers.facebook.com/docs/reference/api/user/) However when I do this, I receive the message in the response object -
An active access token must be used to query information about the current user.
Does anyone have any insight on the matter?
Thanks a ton!
EDIT - the code I'm using:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function (response) {
console.log('Good to see you, ' + response.name + '.');
console.log(response);
});
}

My finding so far indicate that basic information cannot be accessed unless one has either 'userid' or 'username' as in this post.
If one has to try and fetch basic information using following url, then access_token is needed
facebook
{
"error": {
"message": "An active access token must be used to query information about the current user.",
"type": "OAuthException",
"code": 2500
}
}

Related

Facebook Graph API private_replies with page access token throws an error

I'm trying to send private replies as a page with the Graph API:
FB.api( '/' + id + '/private_replies', 'post',
{ message: message, access_token: token }, function(res) {}
);
The comment id is fine. The access token has been generated correctly. I have read_page_mailboxes permission. Despite all that, the API throws back this:
{
"error": {
"message": "(#10903) This user cant reply to this activity",
"type": "OAuthException",
"code": 10903,
"fbtrace_id": "BW3yOdmwnhi"
}
}
Am I missing something?
The problem appears to be the fact that I tried to reply to my own comments. That is not allowed. Only comments from others are allowed to reply to. Also, once replied to, the comment cannot be replied to ever again. That will throw a (different) error as well.
I also encountered the same problem. The reason might be your page is not published yet.

Error generating App Token Facebook Javascript SDK

I'm trying to generate a App Token.
What I understood when I read the documentation, is that this is a GET call
GET /oauth/access_token?
client_id={app-id}
&client_secret={app-secret}
&grant_type=client_credentials
So I decided to test it on the Graph API Explorer
Having my url the following way:
oauth/access_token?client_id={'XXXXXXXXXXXXX'}&client_secret={'XXXXXXXXXXXXXXXXX'}&grant_type=client_credentials
But I'm getting this error:
The client_id I git it from the dashboard so I don't know why.
{
"error": {
"message": "Error validating application. Invalid application ID.",
"type": "OAuthException",
"code": 101
}
}
So I don't know what I'm missing or confusing.
I have this function that's basically the same, but using the api and getting and having a different error.
Function
function token(){
FB.api('/oauth/access_token', 'GET', { client_id: 'xxxxxxx',client_secret: 'xxxxxxxx',grant_type: 'client_credentials' }, function(response) {
if (!response || response.error) {
console.log(response);
alert('Error occured');
} else {
token1=response;
console.log(response);
}
});
}
Error
Uncaught ReferenceError: RwuSu43k_afkprBJ8BuwlCDuxdA is not defined access_token?access_token=CAAEceA3wr9MBALoGOmjrSyhJAeZCWWT301Tf5Ypl8xPL9WCgzTJbusfTPpzrda475mVQznUf…:1(anonymous function) access_token?access_token=CAAEceA3wr9MBALoGOmjrSyhJAeZCWWT301Tf5Ypl8xPL9WCgzTJbusfTPpzrda475mVQznUf…:1
Object {error: Object}error: Objectmessage: "unknown error"type: "http"__proto__: Object__proto__: Object index.html:84
Any idea why I'm getting this?
According to documentation, I can just pass the app id, but I want to generate the token.
There is another method to make calls to the Graph API that doesn't
require using a generated app token. You can just pass your app id and
app secret as the access_token parameter when you make a call
Like #luschn said, an App Access Token is only the App ID concatinated with the App Secret by a pipe symbol:
{app_id}|{app_secret}
You don't need to "generate" id. You can use the app's data out of the box.

FB.api error when fb.getLoginStatus works

very frustrated with javascript SDK for facebook.
can someone tell me why this works (gives an alert with my facebook name):
FB.getLoginStatus(function(response) {
if (response.authResponse) {
token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
// do something here they are logged in and have given you perms
});
} else {
// no user session available, someone you dont know
}
});
But this returns an error, "An active access token must be used to query information about the current user.":
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
});
But this returns an error, "An active access token must be used to query information about the current user."
Well, because you haven’t acquired an access token before calling the method …?
FB.getLoginStatus provides you with a fresh user access token, provided the user has authorized your app before.
But if you skip this step of interaction with Facebook – where do you expect a valid access token to come from?
Right, there is none – and therefore you are getting the error message, stating exactly that …

Why does my FB.api request always produce an error response?

I have a Facebook app with namespace 'thenovel'. It has an action 'rate' assigned to an object 'sentence'. When the 'dofacebookstuff()' function is called, I want the user's rating of a sentence - 'testsentence', for the moment - to be published to their timeline. 'Rating' doesn't require an access token.
Whenever the dofacebookstuff() is called, the error alert pops up. I have tested whether the '!response' or 'response.error' was causing it, and it is the 'response.error' every time.
I can get the user id, name etc. fine with FB.login but the api call just does not work. If I go straight to 'https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX', I see the following:
{
"data": [ ],
"paging": {
"next": "https://graph.facebook.com/me/thenovel:rate?sentence=testsentence&access_token=XXXXX&offset=25&limit=25"
}
}
I have checked my HTML and all the Facebook-related stuff is there and in the right place. None of the other scripts on the page produce errors. Here is the function in question:
function dofacebookstuff() {
FB.api(
'/me/thenovel:rate?sentence=hello&access_token=' + accesstoken,
'post',
function(response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Action ID: ' + response.id);
}
});
}
Could anyone explain where I'm going wrong? Where is my response data?
If you're posting to /me you need an active access token for that user - do you have one? it may have expired already, but i think the message is specifically referring to the fact that you haven't provided one, as there'd be a specific message if the token is invalid or expired.
You should be able to post to /USER_ID/thenovel:rate with the app access token if you're doing offline publishing of some kind
Try logging in using FB connect first.

FQL Javascript Access Token

I've pretty new to using FQL in any form, sorry if this is a simple question, but I can't seem to get around it.
I have this code that's attempting to get a Friend list:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id);
But the response I keep getting back is
"An active access token must be used to query information about the current user."
When I pull back the response object from the /me query I see all my information such as location, name, work, etc. But I don't see any access token
The code starts like this:
window.fbAsyncInit = function() {
FB.init({appId: '12...0', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
How do I acquire an access token and provide along with the FQL query?
Thanks for any advice ahead of time!
The access token that you have to use in order to do all types of requests to the Graph API (including FQL queries) is given by facebook after the user has succesfuly logged in.
If you are using firebug do a console.log(response) after the user logs in to see the object that you receive.
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
// do something with response
login();
});
It should contain 3 properties: perms, session and status. perms contains a list with permissions granted by the user, the status it's the status of the current user. What matters to you is the session property. This one it's also an object that among others has a access_token property (string) which you can use to do requests to API.
So, your query may look like this:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id + '&access_token='+response.session.access_token);
If you want to get the session in a better way, use FB.getSession, which returns the current session. More info here.
Good luck !

Categories