Create a Request in Facebook SDK 4 (Android) - android

As I understand, Facebook SDK v4 doesn't use the Session anymore, but I read in their Graph API document, the session is still used to create the Request
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3
/* make the API call */
new Request(
session,
"/me",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
It's confusing me, and how can I get the session for Request? Any latest example to get the user profile data by using Facebook Graph API?
Any advice is appreciated.

On facebook developers' page is published a changelog and upgrade post. In your case this would be an option:
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse response) {
/* handle the result */
}
}).executeAsync();
By the way, they specify this:
By default a newMeRequest method fetches default fields from a user object. If you need any additional fields, or want to reduce the response payload for performance reasons, you can add a fields parameter and request specific fields.
For example:
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
I hope my answer helps you, being this one my first answer in stackoverflow. Greetings.
Reference: Facebook Android SDK Upgrading

Related

How can my Android app use Facebook's API to know if a user liked a post?

I did not found in documentation how can Facebook's API tell me if my user with Facebook logged, has liked a post of a specific page. Is the API of Facebook able to inform my app of such action? Maybe you can tell me?
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{person-id}/",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();

Android Facebook SDK: how to retrieve email if not public

I created a few dummy facebook accounts. When I use the Graph API Explorer (https://developers.facebook.com/tools/explorer/), for some accounts it returns the email, for other accounts it does not. And when I use the Android and sign in through facebook, it does not return an email for those accounts, despite the fact that I explicitly asked for the email:
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture.type(large)");
There is no email in the returned object:
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Is there any way to be guaranteed to get then email?
No, there is no way, because a Facebook user may have no email at all. You can register at Facebook simply using a phone number.
Can be found in the Facebook docs:
https://developers.facebook.com/docs/facebook-login/permissions/#reference-email

How can we see users’ information through Facebook Graph API in Android?

I am making a dating app (android) by using Facebook API (You know the similar application that starts with tin…)
Anyways, I registered my application in Facebook developers and I also got approval to use functions that I need through 'Submit Items for Approval'(my Approved Items: email, public_profile, User_photo, User_like, user_work_history, user_education_history )
So, I can log in from my app using my Facebook ID and I can also see users’ basic information too.
But! The main problem is that I cannot see photos (except public_profile) even though I was approved for this.
So I really need someone’s help. Please help me!
It will be great if you can review the code I used (To use when looking at users’ photos)
new Request(Session.getActiveSession(), "/{user-id}/albums", null, HttpMethod.GET, new Request.Callback() {
#Override
public void onCompleted(Response response) { }
}).executeAsync();
Hey Use the following code. It works perfectly for me.
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "photos.limit(100)");
request.setParameters(parameters);
request.executeAsync();
Try the above code and you will get a jsonobject which can be used to get the image URLs. To know the response you get. Try Graph API explorer. The response format will be the same.
https://developers.facebook.com/tools/explorer
if you would like to get the photos of a user try the following code. Please note that you need the user-id to they photos
new Request(
session,
"/{user-id}/photos",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
Please refer to https://developers.facebook.com/docs/graph-api/reference/user/photos/
You can use facebook graph api for accessing user info
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
To call this method, use below line,
request.executeAsync();
For additional info visit facebook official documentation
https://developers.facebook.com/docs/android/graph

Android Facebook SDK 4.0 - Display username

I've been having a little trouble migrating to FB SDK's new version.
According to the documentation, this is how you fetch default user data:
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
And I believe that the data is contained withing the JSONObject.
However, I do not know how I can access and then use that user data.
Any help would be extremelly appreciated!
Thanks!
Now facebook sdk 4.0 doesnt return GraphUser it returns pure JSON object
While working around with my android app i found someof the tags. Check this
{"id":"10000700XXXXXXX","birthday":"XX\/XX\/XXXX","email":"xxxxxx#gmail.com","first_name":"xxx","gender":"male","last_name":"xxxxx","link":"http:\/\/www.facebook.com\/10000700XXXXXXX","location":{"id":"1064427060XXXXX","name":"XXXX, XXXXXXXXXXX"},"locale":"en_US","name":"XXX XXXXX","timezone":5.5,"updated_time":"2015-03-27T13:33:02+0000","verified":true}
*************************************
public void onCompleted(JSONObject user, GraphResponse response) {
String id = user.optString("id");
String firstName = user.optString("first_name");
String lastName = user.optString("last_name");
and so on...
Request is executing in another thread, so you cannot write this data in array or variable, that initialized not in this thread. Instead you can set this data in any view you need. All time, when you want to write data between different threads, after operation you will have null object. Good luck)

Get Posts data from facebook in android

I want to get all the posts of my facebook account wall in android. I have got session but when I execute the below query then it gives an error.
new Request(fbSession,"/me?fields=posts",null, HttpMethod.GET, new Request.Callback()
{
public void onCompleted(Response response) {
ShowToast(response.toString());
}
}).executeAsync();
Error:
Active Access Token must be used to query information about the current user
Although I have an active session variable, which can retrieve my basic information
1) It means you don't have an access token or access token does not match the current logged in user.
2) You have an expired or non-working access token (perhaps left over from the last time you used the app?). You could try clearing that out and re-authenticating.
3)
if (state.isOpened())
{
new Request(fbSession,"/me?fields=posts",null, HttpMethod.GET, new Request.Callback()
{
public void onCompleted(Response response) {
ShowToast(response.toString());
}
}
).executeAsync();
}
for more info see below link:-
http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/
https://developers.facebook.com/docs/android/getting-started/

Categories

Resources