Android - Get profile cover picture from Facebook - android

I am using Facebook SDK and Parse SDK and I want to retrieve the profile cover picture.
I am doing the following:
new Request(
ParseFacebookUtils.getSession(),
"/me?fields=cover",
null,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
Log.wtf("TAG",
response.toString());
}
}).executeAsync();
But I am not able to get the proper response since it says I need an access token (the user has already been logged in).
{Response:
responseCode: 400,
graphObject: null,
error: {
HttpStatus: 400,
errorCode: 2500,
errorType: OAuthException,
errorMessage: An active access token must be used to query information about the current user.
},
isFromCache:false
}
Is there any fix for this available?

After spending A LOT of hours searching for the answer, I finally got it !!!
The Android SDK for Facebook docs, are too useless.
To solve this problem we just need to set the Graph Path in the second param and a Bundle with fields as third param. Example:
Bundle params = new Bundle();
params.putString("fields", "cover");
new Request(ParseFacebookUtils.getSession(),
"me",
params,
HttpMethod.GET,
new Request.Callback() {
#Override
public void onCompleted(Response response) {
//code...
}
}).executeAsync();
Then we can parse the response object returned in onCompleted as JSON with
response.getGraphObject().getInnerJsonObject();
//or
response.getGraphObject().getProperty("cover");
Source: New Facebook SDK and OAuthException in Graphpath requests thanks to #Jesse Chen

Facebook changed a few things and has some terrible documentation. Hope this helps someone else it's what worked for me.
public void getCoverPhotoFB(final String email, AccessToken accessToken){
if(!AccessToken.getCurrentAccessToken().getPermissions().contains("user_photos")) {
Log.e(L, "getCoverPhotoFB....get user_photo permission");
LoginManager.getInstance().logInWithReadPermissions(
this,
Arrays.asList("user_photos"));
}
////
Bundle params = new Bundle();
params.putBoolean("redirect", false);
params.putString("fields", "cover");
new GraphRequest(
accessToken,
"me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(final GraphResponse response) {
Log.e(L, "getCoverPhotoFB..."+response);
// thread is necessary for network call
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
String picUrlString = (String) response.getJSONObject().getJSONObject("cover").get("source");
Log.d(L,"getCoverPhotoFB.....picURLString....."+picUrlString);
URL img_value = new URL(picUrlString);
Bitmap eventBitmap = BitmapFactory.decodeStream(img_value.openConnection().getInputStream());
saveImageToExternalStorage(eventBitmap, email + "_B.png");
homeProfile(profile, email);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
thread.start();
}
}
).executeAsync();
}

Related

Why do I get an empty list of friends? Facebook SDK Android

My Android Code:
loginButton.setReadPermissions("user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
profile = Profile.getCurrentProfile();
Log.e("AAAAA=onSuccess", profile.getId());
GraphRequestAsyncTask graphRequestAsyncTask = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/friends",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
try {
JSONArray rawName = response.getJSONObject().getJSONArray("data");
Log.e("AAAAA=onCompleted", rawName.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}).executeAsync();
Why do I get an empty list of friends?
{
"data": [
]
}
I only have 1 each . But I get an empty list . Why is this happening? Tell me what should I do ?
Applications created after April 30th 2014 uses version 2.0+ of the Graph API, which will only return your user's friends that have you app installed. There is no way to retrieve all the friends of you user. It is against Facebook's privacy policy. More Info

(#803) Some of the aliases you requested do not exist: {education-experience-id} Android

I am trying to get education detail and work description of user from facebook. I login successfully and get Access token. But I am unable to get details I want
Code I am using for it :-
public void getUserExpandEducation() {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/{education-experience-id}", //"/{user_education_history}",//
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.d("fb response",response.toString());
}
}
).executeAsync();
}
can anyone please reply
I am getting error (#803) Some of the aliases you requested do not exist: {education-experience-id}
Finally I got full work and education detail by this code:
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
FirstNameSocial = object.optString("first_name");
LastNameSocial = object.optString("last_name");
GenderSocial = object.optString("gender");
EmailSocial = object.optString("email", "");
id = object.optString("id");
if (!EmailSocial.equals("")) {
login_type = Config.Login_Type_facebook;
callAPI(EmailSocial, id, "");
} else {
Toast.makeText(getApplicationContext(), "Permision Denied", Toast.LENGTH_LONG)
.show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,birthday,gender,first_name,last_name,picture,education,work");
request.setParameters(parameters);
request.executeAsync();
Might help someone !
Happy coding :)
Make sure you authorized with that permission: user_education_history
API call to get a list of education IDs: https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Deducation
In your code, you need to replace the following string with one of the resulting education IDs: {education-experience-id}
For example:
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/12345",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.d("fb response",response.toString());
}
}
).executeAsync();

How to get Facebook Feeds (post) using facebook sdk 4.0 in android?

I have show facebook feeds(post) list in may application. I am using facebook 4.0 I am able to get profile but unable to fetch all my feeds(post). If anybody have any reference then please tell me. Thanks.
Right-now I am using following code for get Feeds but getting blank response like {"data":[]}
FacebookCallback<LoginResult> facebookCallback = new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
new GraphRequest(
AccessToken.getCurrentAccessToken(),
loginResult.getAccessToken().getUserId()+"/feed/",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
Log.i("fb", "Feeds :" +response.getJSONObject());
Toast.makeText(
getApplicationContext(),
response.getJSONObject()
+ "",
Toast.LENGTH_SHORT).show();
}
}
).executeAsync();
}
You need to make a graph api call from within your app https://developers.facebook.com/docs/graph-api/reference/v2.3/user/feed
Here,
You are not passing parameters like these:
Bundle params = new Bundle();
params.putString("fields", "message,created_time,id,full_picture,status_type,source,comments.summary(true),likes.summary(true)");
params.putString("limit", "10");
/* make the API call */
new GraphRequest(AccessToken.getCurrentAccessToken(), "/userId/posts", params, HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
/* handle the result */
System.out.println("Festival Page response::" + String.valueOf(response.getJSONObject()));
try {
JSONObject jObjResponse = new JSONObject(String.valueOf(response.getJSONObject()));
}
catch (Exception e) {
e.printStackTrace();
}
}
}
).executeAsync();
Hope to solving these issue i am solving into facebook SDK 4.6.0
You should add the user_posts permission to your login request:
LoginManager.getInstance().logInWithReadPermissions(this,
Arrays.asList("public_profile ", "user_status","user_posts"));

Android - Facebook API : post image

I'm building an app that allows Facebook users to create new events. I noticed that I can't create an event and add an image to it in a unique Graph API call. So I make another call to post the image:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
Bundle params = new Bundle();
params.putParcelable("source", bitmap);
Request postImageRequest = new Request(Session.getActiveSession(), eventId + "/picture", params, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
Log.e("", response.toString());
finish();
}
});
postImageRequest.executeAsync();
But but I get this error response from FB servers:
HttpStatus: 400, errorCode: 324, errorType: OAuthException,
errorMessage: (#324) Missing or invalid image file}, isFromCache:false
Request photoImageRequest= Request.newUploadPhotoRequest(Session.getActiveSession(), bitmap, new Request.Callback(){
#Override
public void onCompleted(Response response) {
Log.e("", response.toString());
finish();
}});

New Facebook SDK and OAuthException in Graphpath requests

I saw a buch of answers regarding this problem with the older versions of the SDK, and I can't seem figure out why this is happening to me.
if I use this code, it works perfectly:
String QUERY = "select uid, name, pic_square, is_app_user from user where uid in (select uid2 from friend where uid1 = me())";
protected void getFacbookFirends() {
Bundle params = new Bundle();
params.putString("q", QUERY);
final Request req = new Request(getSession(), "/fql", params, HttpMethod.GET, fbCallback);
runOnUiThread(new Runnable() {
#Override
public void run() {
Request.executeBatchAsync(req);
}
});
}
but this is very ugly, so I tried using this instead:
Session session = Session.getActiveSession();
if (session == null) {
session = new Session(getActivity());
}
if (session != null && session.isOpened()) {
Request req = Request.newGraphPathRequest(session, "/me/friends?fields=id,name,installed,picture",
new Callback() {
#Override
public void onCompleted(Response response) {
Log.w("FriendsListFragment.Facebook.onComplete", response.toString());
}
});
Request.executeBatchAsync(req);
}
to my understanding, this is the exact same request and should run just the same way, but instead of getting the response I wanted, I get this Response object:
{Response:
responseCode: 400,
graphObject: null,
error: {FacebookServiceErrorException:
httpResponseCode: 400,
facebookErrorCode: 2500,
facebookErrorType: OAuthException,
message: An active access token must be used to query information about the current user.
},
isFromCache:false
}
any thoughts about how I can make this work nicely?
EDIT:
I tried running this code and still got the same result:
Request req = new Request(session, "/me/friends?fields=id,name,installed,picture",null, HttpMethod.GET,
new Callback() {
#Override
public void onCompleted(Response response) {
Log.w("FriendsListFragment.Facebook.onComplete", response.toString());
}
});
Request.executeBatchAsync(req);
Request req = new Request(session, "/me/friends?fields=id,name,installed,picture",null, HttpMethod.GET, .......
Don't put the entire path in the graph path parameter, everything after the ? should be in the params parameter that you set to null. Try this instead:
Bundle params = new Bundle();
params.putString("fields", "id,name,installed,picture");
Request req = new Request(session, "me/friends", params, HttpMethod.GET, .......
That will do the trick.

Categories

Resources