I am using Facebook Login for Android. It works. I can log in through the emulator. However, I need to send back information to my Rails Rest API. Upon facebook callback success, I want to send back the access token, the provider ("facebook"), the uid, facebook user name, facebook user email and the facebook image icon.
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i("AUTHENTICATION TOKEN", String.valueOf(loginResult.getAccessToken()));
}
Logcat shows the following:
I/AUTHENTICATION TOKEN: {AccessToken token:ACCESS_TOKEN_REMOVED permissions:[public_profile, contact_email, email]}
How do I get the information I need onSuccess?
Once you have the access token obtained using LoginResult.getAccessToken(),
You need to call the Facebook Graph api using the access token received. From the SDK Docs,
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,email");
request.setParameters(parameters);
request.executeAsync()
The JSONObject object will contain the required user information provided the user has granted all the necessary permissions to your application
The list of fields available for user can be seen here
try this
Log.i("AUTHENTICATION TOKEN", AccessToken.getCurrentAccessToken().getToken());
Related
Can I fetch all the posts data from a page with logged in user's access token ? or Is there any other way to retrieve all the posts data of a public page using Facebook Graph Api for Android ?
You will get facebook post data in json format that you can parse according to your requirement.
GraphRequest request = GraphRequest.newGraphPathRequest(
AccessToken.getAccessToken(),
"/YOUR_PAGE_ID/posts",
new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
// Insert your code here
Log.d("raw response ",response.getRawResponse());
}
});
request.executeAsync();
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
I am trying to fetch the friend list using the code below
GraphRequest request = GraphRequest.newMyFriendsRequest(
token,
new GraphRequest.GraphJSONArrayCallback() {
#Override
public void onCompleted(JSONArray array, GraphResponse response) {
// Insert your code here
}
});
request.executeAsync();
}
The response returned after code execution is
{Response: responseCode: 200, graphObject: {"data":[],"summary":{"total_count":461}}, error: null}
Persmission to access friends and public profile is granted on login time.
How can i fetch my friend list and their public emails/ facebook emails?
You'll only receive the friends whcih are also using your app, see
https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids
/me/friends returns the user's friends who are also using your app
In v2.0, the friends API endpoint returns the list of a person's friends who are also using your app. In v1.0, the response included all of a person's friends.
And, no, there is no workaround in case you wanted to ask.
You can recive your friends list using graph-api call
/* make the API call */
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
But the limitation is
api required access token with user_friends permission
only return friends who are also given access permission to the same app you are
using
.
https://developers.facebook.com/docs/graph-api/reference/v2.5/user/friends
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
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