Get Facebook friends list using openActiveSession or REST API - android

I'm developing an Android app with latest facebook SDK and I'm using this code to get some data from facebook:
public void onFBLoginClick(View view)
{
openActiveSession(this, true, statusCallback);
}
Session.StatusCallback statusCallback = new Session.StatusCallback()
{
#Override
public void call(final Session session, SessionState state, Exception exception)
{
if(session.isOpened())
{
Request.executeMeRequestAsync(session, new Request.GraphUserCallback()
{
#Override
public void onCompleted(GraphUser user, Response response)
{
if(user != null)
{
txtUserName.setText(session.getAccessToken());
String gender = user.getProperty("gender").toString();
String email = user.getProperty("email").toString();
saveUserData(user.getId(), user.getName(), user.getBirthday(), user.asMap().get("email").toString());
saveAccessToken(session.getAccessToken());
getFacebookUserProfilePicture(session.getAccessToken());
}
}
});
}
}
};
private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback statusCallback)
{
OpenRequest openRequest = new OpenRequest(activity);
openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
openRequest.setCallback(statusCallback);
Session session = new Session.Builder(activity).build();
if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
{
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
Using this code, how can I get friends list? Or, do I have to use REST API to do it?

Related

Android FacebookSDK get Birthday

I have created sample app with facebook sdk integration. Now I can Login with facebook Also get some basic information from facebook. But fail to got birthday from "GraphUser" I have noticed that for birthday I need to set permission "user_birthday" which I have already set. I couldn't get what wrong with response.
private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List permissions) {
Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
private void startFacebookLogin() {
openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
//make request to the /me API
Log.e("sessionOpened", "true");
Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String firstName = user.getFirstName();
String lastName = user.getLastName();
String id = user.getId();
String email = user.getProperty("email").toString();
Log.d("AllData", user.toString());
Log.e("facebookId", id);
Log.e("firstName", firstName);
Log.e("lastName", lastName);
Log.e("email", email);
String Birthdate=user.getBirthday();
Log.e("Birthdate", Birthdate+"::Birthdate");
}
}
}).executeAsync();
}
}
}, Arrays.asList(
new String[] { "email", "user_location", "user_birthday","user_likes" }));
}
GraphUser respose is
GraphObject{graphObjectClass=GraphUser, state={"id":"…..","first_name":"…..","timezone":5.5,"email":"…..#gmail.com","verified":false,"name":"…..","locale":"en_US","link":"https:\/\/www.facebook.com\/app_scoped_user_id\/....\/","last_name":"…..","gender":"male","updated_time":"2012-12-21T06:54:19+0000"}}
Are you trying this with an admin/tester/developer user of your Facebook app? If not, the reason is that if you request extended permissions, you'll need to get your app through the Facebook app review process, as described here:
https://developers.facebook.com/docs/apps/review/login#do-you-need-review

Getting email from GraphUser with Permissions

I'm attempting a log-in with FB with the code below. The GraphUser object has first name, last name, ID, but not email. I know I need to add permissions but how do I do it in this case. The permissions are generally added with Session.OpenRequest or the LogInButton but I'm not using those. Any suggestions?
Session.openActiveSession(activity, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// get email from GraphUser
}
}
}).executeAsync();
}
}
});
I you have more than one permissions then make a list of permissions and call the performPublish() method (where you will be passing that Permission list as parameter) to publish a permission and with that check for the publish permission.
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions", "email");
private void performPublish() {
Session session = Session.getActiveSession();
if (session != null) {
if (hasPublishPermission()) {
postStatusUpdate("name");
} else {
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSIONS));
}
}
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
Just add the permissions as a parameter in openActiveSession:
Session.openActiveSession(activity, true, Arrays.asList("public_profile", "email"), new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// get email from GraphUser
}
}
}).executeAsync();
}
}
});

android facebook sdk - set permissions for openActiveSession

I am trying to integrate Facebook SDK to an android app. I got the code from Facebook manual. It uses Session.openActiveSession and then request a graph user. How could I request for more permissions without using LoginButton class?
Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
if (session.isOpened()) {
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
// got user graph
} else {
// could not get user graph
}
}
});
}
}
});
Thank you.
Try this:
mCallback = new Session.StatusCallback() {...}; // the code you already have
Session.OpenRequest request = new Session.OpenRequest(mContext);
request.setPermissions(Arrays.asList("email", "user_birthday"));
request.setCallback(mCallback );
// get active session
Session mFacebookSession = Session.getActiveSession();
if (mFacebookSession == null || mFacebookSession.isClosed())
{
mFacebookSession = new Session(mContext);
Session.setActiveSession(mFacebookSession);
}
mFacebookSession.openForRead(request);
This solves the problem using opensession and extended persmission just once. Facebook SDK 3.5
Session s = new Session(this);
Session.setActiveSession(s);
Session.OpenRequest request = new Session.OpenRequest(this);
request.setPermissions(Arrays.asList("basic_info","email"));
request.setCallback( new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Request.newMeRequest(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
Toast.makeText(getApplicationContext(), "User email is:"+user.getProperty("email"), Toast.LENGTH_SHORT).show(); }
else {
Toast.makeText(getApplicationContext(), "Error User Null", Toast.LENGTH_SHORT).show();
}
}
}).executeAsync();
}
}
}); //end of call;
s.openForRead(request); //now do the request above

how to get email id from facebook sdk in android applications?

I integrated Facebook login in my android application. I want to get email id of login user. How will I get it?
private void loginToFacebook() {
Session.openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
try {
userID =user.getId();
provider="facebook";
userName=user.getUsername();
firstName=user.getFirstName();
lastName=user.getLastName();
Log.d("****User****", "details:" + user);
}catch(Exception e){
Here is my code. i use Request.GraphUserCallback() method but there is no response of email from this method.
Before calling Session.openActiveSession do this to get permissions add this:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
The last parameter in Session.openActiveSession() should be permissions.
Now you can access user.getProperty("email").toString().
EDIT:
This is the way I am doing facebook authorization:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
loginProgress.setVisibility(View.VISIBLE);
//start Facebook session
openActiveSession(this, true, new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
//make request to the /me API
Log.e("sessionopened", "true");
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String firstName = user.getFirstName();
String lastName = user.getLastName();
String id = user.getId();
String email = user.getProperty("email").toString();
Log.e("facebookid", id);
Log.e("firstName", firstName);
Log.e("lastName", lastName);
Log.e("email", email);
}
}
});
}
}
}, permissions);
Add this method to your activity:
private static Session openActiveSession(Activity activity, boolean allowLoginUI, Session.StatusCallback callback, List<String> permissions) {
Session.OpenRequest openRequest = new Session.OpenRequest(activity).setPermissions(permissions).setCallback(callback);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
Following the suggestion of Egor N, I change my old code
Session.openActiveSession(this, true, statusCallback);
whith this new one:
List<String> permissions = new ArrayList<String>();
permissions.add("email");
Session.openActiveSession(this, true, permissions, statusCallback);
Now FB ask the user about the permission of email and I can read it in the response.
Try this article. Hope your issues will be solved Click Here
Btw, You need to use user.asMap().get("email").toString()); for receving the User Email ID.
Also, you need to assign that into some of label like lblEmail.setText where lblEmail is a Textview.

Activity com.facebook.LoginActivity has leaked window

I am having some difficulty in Facebook login which was working perfectly fine 1 week ago.
I am using facebook sdk 3.0.1 and this is my code
private void fbfunc() {
login.setEnabled(false);
Session.StatusCallback fbStatusCallback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) { // callback for session state changes
if (state.isOpened()) {
Request.executeMeRequestAsync(session,
new GraphUserCallback() {
#Override
public void onCompleted(GraphUser user,
Response response) {
login.setEnabled(true);
JSONObject jsonObj = user
.getInnerJSONObject();
dat = jsonObj.toString();
if(pdialog!=null)
pdialog.dismiss();
fblog fb = new fblog();
fb.execute();
System.out.println(">>>>>>>>>" + dat);
}
});
}
}
};
openActiveSession(this, true, fbStatusCallback,
Arrays.asList("email", "user_birthday","user_hometown","user_location"));
}
private static Session openActiveSession(Activity activity,
boolean allowLoginUI, StatusCallback callback,
List<String> permissions) {
OpenRequest openRequest = new OpenRequest(activity)
.setPermissions(permissions)
.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK)
.setCallback(callback)
.setDefaultAudience(SessionDefaultAudience.FRIENDS);
Session session = new Session.Builder(activity).build();
if (SessionState.CREATED_TOKEN_LOADED.equals(session.getState())
|| allowLoginUI) {
Session.setActiveSession(session);
session.openForRead(openRequest);
return session;
}
return null;
}
Now it's giving me exception
Activity com.facebook.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#405657c0 that was originally added here
It's really strange because it was working perfectly.

Categories

Resources