Facebook Graph request Android name from user ID - android

I'm trying to get the name of a user from his user ID.
on this link they say that we can do it from a simple HTTP request like that:
http://graph.facebook.com/4
But it seems that this method is outdated because i cant get anything because:
"An access token is requir…o request this resource."
Anyway i tried using their documentation about the graph api on Android and i did it like that:
GraphRequestAsyncTask request1 = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/"+id1+"/name",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
name1.setText(request1.toString());
But on the name1 TextView i get a message that tells me
{RequestAsyncTask:
connection:null,requests:[{Request:
accesToken:{AccessToken
token:ACCESS_TOKEN_REMOVED permissions:
[public_profile]},
graphPath:/100017723671435/name, graphObject: null,
httpMethod:Get,
parameters:Bundle[{}]}]}
I don't really understand how to do, i have a bunch of facebook ID's on my database and i just want to get the name from the ID, to display it on the screen.
The current user of the app is not the profile which i want to get the infos!
EDIT: It seems i misunderstood the /*handle the result*/ commentary, i made this line like that:
name1.setText(response.toString());
But i have still an error on my TextView:
{Response:
responseCode:400,
graphObject:null,
error: { HttpStatus:400,
errorCode: 2500,
errorType: OAuthException,
errorMessage: Unknown path components: /first_name }}
So it seems i don't use the Graph Paths properly. I'm still looking on the doc and on Google, if i find the answer i'll give the code!

Ensure that you are logged in with read permission.
If you are logged in you can try with:
GraphRequest.Callback gCallback = new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.d(TAG, "onCompleted: ");
if (response != null && response.getJSONObject() != null && response.getJSONObject().has("first_name"))
{
try {
name1.setText(response.getJSONObject().getString("first_name"));
} catch (JSONException e) {
Log.e(TAG, "onCompleted: ",e );
}
}
}
};
new GraphRequest(AccessToken.getCurrentAccessToken(),"/me?fields=id,name,gender,email,first_name,last_name", null,HttpMethod.GET, gCallback).executeAsync();
If you have a problem with login, maybe this link can help you.

Related

GraphRequest.newMyFriendsRequest return empty list android facebook sdk 4.0+

I am using the following login method to login
ParseFacebookUtils.logInWithReadPermissionsInBackground(this,
Arrays.asList("user_status", "read_stream", "user_friends", "read_friendlists"), new LogInCallback() {
#Override
public void done(ParseUser parseUser, ParseException e) {
System.out.println("ParseUser: " + parseUser);
System.out.println("ParseException: " + e);
linkUser(parseUser);
}
});
and using the following method to get my friend list, I need to retrieve all friends
GraphRequest request1 = GraphRequest.newMyFriendsRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONArrayCallback() {
#Override
public void onCompleted(JSONArray jsonArray, GraphResponse graphResponse) {
Toast.makeText(HomeScreenActivity.this, "" + jsonArray.length(), Toast.LENGTH_SHORT).show();
}
});
request1.executeAsync();
but the jsonArray is always empty, can anyone tell me what is wrong here ?
EDIT
I also get the following value in the Graph Response
{Response: responseCode: 200, graphObject: {"summary":{"total_count":476},"data":[]}, error: null}
As from Facebook api docs, this code would return all your friends who have logged in using Facebook in your android app. I also faced the same issue but after my friends installed my android app into their devices and logged in with Facebook in the app, this code returned list of all those friends with name and their Facebook ids.

Android error getting Facebook friends with some users

I'm trying to get the friends from facebook, but when I'm try with my personal account I get the friends that I have and uses the app. The problem is when I use an other account (this account has my personal account as friend) the app doesn't get the friends.
This is my code:
if (session.isOpened() && hasFriendsPermission()) {
Request friendRequest = Request.newMyFriendsRequest(session,
new GraphUserListCallback(){
#Override
public void onCompleted(List<GraphUser> users,
Response response) {
Log.i("INFO", response.toString());// + " - " + users.size());
friends = users;
if (friends != null && friends.size() > 0) {
if (processTask == null) {
processTask = new FriendsProcess();
processTask.execute();
}
}
}
});
Bundle params = new Bundle();
params.putString("fields", "id,name");//,friends");
friendRequest.setParameters(params);
friendRequest.executeAsync();
}
And this is the response I'm getting in the other account (the account that's not my personal facebook account):
{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 100, errorType: OAuthException, errorMessage: Unsupported operation}, isFromCache:false}
I hope you can help me to fix this, or at least to know why this is happening. Thanks!
When your application is in sandbox/development mode you can not perform any api call for other user. To test your for other user, add the user as a tester, developer or admin. See details https://developers.facebook.com/docs/ApplicationSecurity/

how long is the FB postId expiration date?

I write an android application which does a FB like to a FB post.
I succeed in doing it, but after a long time I get this response:
{
Response: responseCode: 400,
graphObject: null,
error:
{
HttpStatus: 400,
errorCode: 100,
errorType: OAuthException,
errorMessage: (#100) Error finding the requested story
},
isFromCache:false
}
for the code:
Request request = new Request(
session,
takeFromPublicMacrosOrServer(currentOffer.postId)
+ "/likes", null, HttpMethod.POST,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
// Request complete
if (response.getError() == null) {
UnlockRequestToServer unlockRequestToServer = new UnlockRequestToServer(
mOffersListActivity,
PublicMacros.TYPE_UNLOCK_FB_LIKE,
currentOffer.postId);
} else {
final String errorMsg = "error: "
+ response.getError()
.toString();
Log.e(MyLogger.TAG, errorMsg);
if (BaseApplication
.getCurrentActivity() != null) {
BaseApplication
.getCurrentActivity()
.runOnUiThread(
new Runnable() {
public void run() {
if (PublicMacros.DEBUG) {
Toast.makeText(
BaseApplication
.getCurrentActivity(),
errorMsg,
Toast.LENGTH_LONG)
.show();
}
}
});
}
}
String re = response.toString();
}
});
request.executeAsync();
Does someone know how long is postId valid for FB like action ?
How do I get a page Access Token that does not expire? May contain some useful iformaion for you.
Facebook 3.0 How to post wall? Shows you how to post to your wall
This Question shows you the 'like' Facebook like in android using android.
EDIT
Facebook doesn't notify you that a previously issued access token has
become invalid. Unless you have persisted the expiry time passed to
your App along with the access token, your app may only learn that a
given token has become invalid is when you attempt to make a request
to the API. Also, in response to certain events that are
security-related, access tokens may be invalidated before the expected
expiration time.

Cannot read a facebook event with graph API from android app

I ma trying to read facebook event's details from an ANdroid application by using this code :
new Request(
session,
"me/events/created",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
System.out.println("Result: " + response.toString());
}
}
).executeAsync();
but I m always receiving a ampty response :
Result: {Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[]}}, error: null, isFromCache:false}
Although when i tried the Graph API Explorer https://developers.facebook.com/tools/explorer i get the right list !!!!
Have you configured the correct permissions for your app? To get the User's Events, you need to aquire the user_events permission upon login.
See https://developers.facebook.com/docs/graph-api/reference/v2.0/user/events/#readperms

Getting 'unsupported method, fql.multiquery' when fetching data from Facebook

I'm trying to fetch data in my Android-App from Facebook via FQL multiquery like discribed here: http://www.kanatorn.info/2011/12/31/fql-multiple-query-android-sdk-example/
here's my code:
Bundle params = new Bundle();
JSONObject multiquery = new JSONObject();
try {
multiquery.put("query1", "SELECT eid, name, start_time, is_date_only, pic_square, pic_big, location FROM event WHERE eid IN" +
"(SELECT eid FROM event_member WHERE uid=me() AND start_time >= now()) ORDER BY start_time");
multiquery.put("query2", "SELECT eid, rsvp_status, start_time FROM event_member WHERE uid=me() AND start_time >= now() ORDER BY start_time");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
params.putString("method", "fql.multiquery");
params.putString("queries", multiquery.toString());
Session session = Session.getActiveSession();
Request request = new Request(session,
"/fql",
params,
HttpMethod.GET,
new Request.Callback(){
public void onCompleted(Response response) {
...
}
});
Request.executeAndWait(request);
the result I'm getting is the following:
{Response: responseCode: 500, graphObject: null, error: {HttpStatus: 500, errorCode: -1, errorType: Exception, errorMessage: Unsupported method, fql.multiquery}, isFromCache:false}
I can't figure out where my mistake is. It says unsupported method, multiquery, but this actually should be a valid method, right? I looked everywhere for a similiar problem but it seems that I'm alone with that issue.
I have the feeling that I'm missing something simple...
Would be cool if someone can help me out. Thanks!
btw. with normal queries everything works fine.
The linked Kanatorn page says method should be fql.multiquery, but you have just multiquery.
To solve this
have you read this developers page.
http://developers.facebook.com/docs/howtos/androidsdk/3.0/run-fql-queries/
Updated url
https://developers.facebook.com/docs/android/graph/#fql

Categories

Resources