how to get user birthday from facebook sdk? - android

How to get user birthday by user.getBirthday() ?
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
TextView welcome = (TextView) findViewById(R.id.textView1);
welcome.setText("Hello " + user.getBirthday() + "!");
Log.d("user_birthday","user_birthday:"+user.getBirthday());//user.getBirthday() this value always is null?
}
}
});
}
}
});

First of all have you set permission for user_birthday? it's very important if you want to access user_birthday information
if the user is not null usually you can get the birthday by user.getBirthday()
because from what I see you're using the new facebook SDK
you can set permission for example when you using the facebook authbutton
authButton.setReadPermissions(Arrays.asList("user_location", "user_birthday", "user_likes"));
or you can reauthorize permission
Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(this, PERMISSIONS).
setRequestCode(REAUTHORIZE_ACTIVITY).
setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
session.reauthorizeForPublish(reauthRequest);
Where The PERMISSION is an array containing your permission

Small tip: It is possible to request permissions with the initial open-session request, thus no need for re-authorization:
final Session.OpenRequest request = new Session.OpenRequest(Activity.this);
request.setPermissions(Arrays.asList("email", "user_birthday")); // Your permissions here
request.setCallback(new Session.StatusCallback()
{
#Override
public void call(final Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
// Your code here
}
}
});
Session fbSession = Session.getActiveSession();
if (fbSession == null || fbSession.isClosed())
{
fbSession = new Session(Activity.this);
Session.setActiveSession(fbSession);
}
fbSession.openForRead(request);
Activity.this - your activity context.
Hope this helps somebody.. :)

Related

Facebook Android Login GraphAPI - how to get users age_range? (max & min)

I have no problem getting all of the other information given to me using the "public_profile" scope. Stuck on getting age range. Also, to be clear I am not looking for "user_birthday" only the "age_range". Thank you.
// set permission list, Don't foeget to add email
authButton.setReadPermissions(Arrays.asList("public_profile", "email"));
// session state call back event
Session.StatusCallback callback = new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
onSessionStateChange(session, state, exception);
if (session.isOpened()) {
Log.i(TAG, "Access Token" + session.getAccessToken());
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) {
// fid = user.getId();
// Log.i("ID", fid);
femail = user.asMap().get("email")
.toString();
Log.i("email", femail);
ffirstname = user.getFirstName();
Log.i("firstname", ffirstname);
}
}).executeAsync();

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

Unable to retrieve email address from Facebook API

I am getting a null pointer exception while getting an email address from Facebook API on Android 3.0.
The code generating the exception is below:
LoginButton authButton;
authButton = (LoginButton) findViewById(R.id.authButton);
// set permission list, Don't forget to add email
authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
authButton.setSessionStatusCallback(new Session.StatusCallback() {
#Override
public void call(Session session, SessionState state,
Exception exception) {
showToast("Inside Call");
if (session.isOpened()) {
showToast("Inside Session");
System.out.println("facebook Access Token"
+ session.getAccessToken());
fbToken = session.getAccessToken();
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
#Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
String email = user.asMap().get("email").toString();
}
}
});
}
}
});
The code was working previously - I might have made a mistake while integrating code.
You can get the user email from the GraphUser Object by using the property.
user.getProperty("email").toString()
It works for me.Hoping it will also work for you.

How to give Facebook permission for accessing user data in facebook-sdk-3.0

I'm developing an application in which I'm using Facebook login for authentication and stuff. facebook sdk 3.0 needs some permission for accessing user data such as profile picture, emailID,publish_stram and etc. How to give permission in the code for accessing those things. Till now I'm able to get fb_access_token. Here is my code:
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
#Override
public void call(Session session, SessionState state,
Exception exception) {
if (session.isOpened()) {
// make request to the /me API
Request.executeMeRequestAsync(session,
new Request.GraphUserCallback() {
// callback after Graph API response with
// user
// object
#Override
public void onCompleted(GraphUser user,
Response response) {
if (user != null) {
fb_user_id = user.getId();
}
Session session = Session
.getActiveSession();
if (session.isOpened()) {
access_token = session
.getAccessToken();
}
new postFBData().execute();
}
});
}
}
});
Taken this code snippet from Facebook samples. How to give permission before accessing access_token from FB?
Any help will be appreciated.
Calling Session.openActiveSession will only give you basic permissions (until you've requested additional ones). You also need to separate the read and publish permission requests.
In your case, I would do something like:
Session session = // create a new Session using Session.Builder
Session.OpenRequest openRequest = // create an OpenRequest using Session.OpenRequest
openRequest.setPermissions( READ_PERMISSION_LIST );
session.openForRead(openRequest);
Session.setActiveSession(session);
Then, once you've opened the session,
// check if you already have publish permissions first
if (!Session.getActiveSession.getPermissions.contains("publish_stream")) {
Session.NewPermissionsRequest permissionRequest = // create a NewPermissionsRequest
permissionRequest.setPermissions( PUBLISH_PERMISSION_LIST);
Session.getActiveSession().requestNewPublishPermissions(permissionRequest);
}
i think this is how you authorize a certain kind of functionality in the String[]
fb.authorize(MainActivity.this,new String[] {"publish_stream"}, new DialogListener(){
#Override
public void onFacebookError(FacebookError e)
{
Toast.makeText(MainActivity.this, "on Facebook error", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(DialogError e)
{
Toast.makeText(MainActivity.this, "on error", Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete(Bundle values)
{
updateButtonImage();
}
#Override
public void onCancel()
{
}
});

Categories

Resources