I am very new to the Facebook development game so please bear with me. I followed the getting started guide on everything to setup the app ( at least I think).
I am trying to do a simple graph request to return info like name of my loggged in user. Below is my code. Everytime I run it the JsonObject returns null, which leads me to believe that I am not connecting. Are their trouble shooting consules or anything on facebooks developer site website that shows requests or that I am connected with the app?
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
if (jsonObject != null) {
JSONObject userProfile = new JSONObject();
Log.i("joe", "dosent appear to have returned nul...");
}
}
});
Please call setGraphPath() for it, something like that:
request.setGraphPath(Constants.YOUR_URL);
looks like your not requesting permissions. on your facebook login button do this:
fbLoginButton.setReadPermissions(Arrays.asList(
"public_profile", "email")); //for example
Related
I want to get user's who is logged into my app friend list. So, I need to use this code to do that:
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
}
}
).executeAsync();
The problem is that I don't know what I need to write into onCompleted method to put friend list to array.
after the data comes to onCompleted() you can do something like this to see the structure of the json response,
Log.d("-#-", "graph response " + response.getJSONObject());
this gives the structure of the json object from there you can get the data.
Also i think your app needs to go through facebook submission as there is other permissions your app needs to get friends of a person
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
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)
I'm having a problem revoking Facebook permissions using the Android SDK.
There's a case in my app where I want to revoke all permissions. According to the Facebook documentation, you can use AsyncFacebookRunner to do this, and "if you don't specify a permission then this will de-authorize the application completely."
I'm currently doing it like this:
String method = "DELETE";
Bundle params = new Bundle();
params.putString("permission", "");
mAsyncRunner.request("/me/permissions", params, method, new RequestListener()
{ ... }, null);
using the request signature like this:
void request(String graphPath, Bundle parameters, final String httpMethod,
RequestListener listener, final Object state)
The onComplete() callback function seems to come back OK, but doesn't appear to have de-authorized the access token. I'm inferring this because the next time I call facebook.authorize(), it works without pushing the user to the Facebook login page.
Any ideas what I need to change to completely de-authorize an access token? Or is there a different/better way to do this? Many thanks!
For anybody looking to do this in later versions of the SDK/Graph API - It appears the correct way to do this now is as shown here https://developers.facebook.com/docs/graph-api/reference/user/permissions/
new Request(
session,
"/me/permissions/{permission-to-revoke}",
null,
HttpMethod.DELETE,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
}
}
).executeAsync();
Leaving the /{permission-to-revoke} off of the second parameter will revoke all the permissions
You can delete the entire application (not only permissions) from users Facebook account using latest SDK (mine is 4.1.1)
void deleteFacebookApplication(){
new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/permissions", null, HttpMethod.DELETE, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
boolean isSuccess = false;
try {
isSuccess = response.getJSONObject().getBoolean("success");
} catch (JSONException e) {
e.printStackTrace();
}
if (isSuccess && response.getError()==null){
// Application deleted from Facebook account
}
}
}).executeAsync();
}
It appears from this post: Facebook deauthorize my app and others that it's not possible to deauthorize an application programmatically. Unfortunately, the call above returns successfully to onCreate() but does nothing to deauth/delete the app for the user.
Bottom line: It looks like the only way to deauth an app is for the user to do it directly in Facebook. If anyone knows differently, please say so - but otherwise, don't waste your time trying! Thanks.
I am using the code suggested in the question and it completely de-authorized my test application. Tested it several times and it worked every each one of them.
This is also the code suggested in the official facebook documentation here: https://developers.facebook.com/docs/mobile/android/build/ - Step 7