How to get user likes from Facebook with Android sdk 3.0 - android

I would get user likes from facebook using Android sdk 3.0 beta.
How do I do this?

Provided that you have a valid session, this is the code to get the user likes
Session session = Session.getActiveSession();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// response should have the likes
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
}
};
Request request = new Request(session, "me/likes", null, HttpMethod.GET, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

Related

How to get facebook likes using Facebook sdk4.0 in android?

Session session = Session.getActiveSession();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// response should have the likes
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
}
};
Request request = new Request(session, "me/likes", null, HttpMethod.GET, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();``
I am trying to get facebook likes using facebook sdk using this code But it is not working.Is there any other way to get it?
Here you can try this code link
it will help you out

Android - Get profile cover picture from Facebook

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();
}

How to share predefined text on Facebook via ShareDialog Android

i have a big problem for sharing text via ShareDialog. It seems i can only share predefined links,descriptions... but not text. Is there any other way to share text of Facebook from my Android app.
My code is:
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() {
return new FacebookDialog.ShareDialogBuilder(this)
.setCaption("Here Comes the Boom!!!!")
.setName("Hello Facebook")
.setDescription("Here is my description")
.setLink("http://developers.facebook.com/android");
}
It's impossible, you have to use the Facebook api to do this.
More informations here: https://stackoverflow.com/a/22123047/2065418
Code for Facebook sharing text/link
public void post(String post_text) {
Bundle postParam = new Bundle();
Request.Callback callback = new Request.Callback() {
#Override
public void onCompleted(Response response) {
// shareDialog.dismiss();
// showPublishResult("Photo Post", response.getGraphObject(),
// response.getError());
}
};
Session session = createSession();
postParam.putString("name", post_text);
postParam.putString("link", "https://developers.facebook.com/android");
Request request = new Request(session, "me/feed", postParam,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}

Post message on my friends wall with sdk 3.5 android

i want to post message on my friends wall , I got my friend list and their Id now i am using the below code but it not posting any message to my any of friend wall, i'll post my code, in my above code i am using a static id of my friends but it is not posting any message on my friends wall , if i replace the id with my fb id then it is posting on my wall?
private void getfriendlist()
{
Session.openActiveSession(this, true, new Session.StatusCallback()
{ // callback when session changes state #SuppressWarnings("deprecation")
#Override
public void call(final Session session, SessionState state, Exception exception) { if(session.isClosed())
{
Log.i("postStatusUpdate session.isClosed", "message not posted session is closed");
} if (session.isOpened())
{ // Log.i("session.isOpened", "session.isOpened"); //session.requestNewReadPermissions(newPermissionsRequest);
if (!session.isOpened())
{ Session.OpenRequest openRequest = null;
openRequest = new Session.OpenRequest(InvitefriendsActivity.this);
if (openRequest != null) { //openRequest.setDefaultAudience(SessionDefaultAudience.FRIENDS); openRequest.setPermissions(Arrays.asList("email,user_birthday,user_location,user_hometown,user_about_me,user_relationships,publish_stream,publish_actions,basic_info,status_update","friends_birthday","read_friendlists")); //openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK); session.openForRead(openRequest);
} }
Request.executeMyFriendsRequestAsync(session, new Request.GraphUserListCallback()
{
#Override public void onCompleted(List<GraphUser> users, Response response) { Log.d("AL",""+response.toString());
for (int i=0;i<users.size();i++)
{ //Log.d("AL",""+users.get(i).toString());
Log.d("AL",""+users.get(i).getId());
Log.d("AL",""+users.get(i).asMap().get("name"));
Bundle params = new Bundle();
params.putString("message", "Hi ");
params.putString("caption", "Hello");
Request request = new Request(session, "100005147887121/feed", params, HttpMethod.POST); RequestAsyncTask task = new RequestAsyncTask(request); task.execute(); } } }); } } }); }
I think this is related to requested Permissions. Are you (you Session) allowed to post to someone else Wall? You will find here a list of available permissions
https://developers.facebook.com/docs/reference/login/extended-permissions/
I think you need to request for 'publish_actions'
Regards
Michael

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