When to Request Permissions with Facebook's New Android Sdk 3.0

When to request permissions with Facebook's new Android SDK 3.0?

I recommend that you read our login tutorial here specifically in step 3. Using the login button that we provide is the most convenient method, (see authButton.setReadPermissions())

EDIT:

To set permissions without using the loginbutton is trickier since you will have to do all the session management by hand. Digging into the source code for the login button, this line of code is probably what you need. It looks like you will need to create your own Session.OpenRequest and set it's attributes such as permissions, audience, and login behavior, then get the current session and call openForRead() on your Session.OpenRequest.

Facebook SDK 3.0 - additional permissions

Might be a late for this answer. I had a similar problem, with the FB SDK 3.0 for Android. It was a bug in the SDK and they've fixed it in FB SDK 3.0.1 for Android released on the 20th of March. The change log mentions:

The Web View Login dialog includes the token information for scenarios where new permissions are being requested. This avoids having the user having to enter their login credentials whenever new permissions are requested for a session.

Try again after you upgrade. Grab the new SDK from here.

Login with extra permission with Facebook SDK 3 for Android

I just answered a similar question on another post. My solution allows you to use a native button and request additional read permissions when the user first logs in. Check it out here Ask for more permissions with 3.0 SDK

Require additional permissions with Facebook Android SDK

One possible idea, though I don't know if there is a precedent for it.

Create a preference in your application called, for example, "Support Email Login". When the user clicks on the Login button and permissions have not been granted, direct them to your preferences screen: "To support this feature, enable it in preferences"

Then, in your preferences screen, when the user checks the "Support Email Login" checkbox, toss up the permission request. If they decline, no permissions have been set and the box remains unchecked. If they accept, check the box and the user can go back and Log in.

This way the feature/permission is configurable by the user, and they don't get spammed with permission requests; they simply get directed to the preferences screen if they want to enable the feature.

ask new read permissions in fb android app login

Finally problem solved in this way. Hope it can be useful for someone.


Inside my activity:

private Session.StatusCallback statusCallback = new
SessionStatusCallback();



onCreate() or onClick() of some button

Session.openActiveSession(MainActivity.this, true, statusCallback);



In the activiy I need to put this class:

private class SessionStatusCallback implements Session.StatusCallback{

@Override
public void call(Session session, SessionState state, Exception exception){
// response to session changes
Toast.makeText(MainActivity.this, "chiamata FB call", Toast.LENGTH_SHORT).show();

if(session.isOpened()) {
/// debug
Log.i(LOG_ACTIVITY,"..session opened..ask for additional permissions");

// additional permissions
List<String> permissions = Arrays.asList("email, user_about_me, user_likes");
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(MainActivity.this, permissions);
session.requestNewReadPermissions(newPermissionsRequest);

//debug
Log.i(LOG_ACTIVITY,"..token:"+ session.getAccessToken());

// request data
Request.newMeRequest(session, new Request.GraphUserCallback() {

@Override
public void onCompleted(GraphUser user, Response response) {
if(user != null){
Toast.makeText(MainActivity.this, "ciao "+user.getName(), Toast.LENGTH_SHORT).show();
}

}

});

}

// check permissions
List<String> fb_permissions = session.getPermissions();
if ( fb_permissions.contains("email")){

// new permission ok, go to next activity
Intent intent = new Intent (MainActivity.this, AfterLogin.class);
startActivity(intent);
finish();
}

else{
// display toast
Toast.makeText(MainActivity.this, "problems with permsissions..", Toast.LENGTH_SHORT).show();
}

}
}

And also this method:

public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(MainActivity.this, requestCode, resultCode, data);
}





And then it works. But I still don't know how to handle the errors or if the user doesn't accept the permissions.
Anyway thanks for the support and hope my work can be useful to someone.

Remember to register the activity as said in Facebook Developer's page (manifest file), and also on the facebook app filling the package and activity name fields.

Facebook SDK 3.7 for Android request email permission

Get the permissions first

private static final List<String> PERMISSIONS = Arrays.asList(
"email","user_location");

The Method for getting the user information

      Session.openActiveSession(RegisterActivity.this, true, new Session.StatusCallback() {

// callback when session changes state
@Override
public void call(Session session, SessionState state,
Exception exception) {
/*
* Session.getActiveSession().onActivityResult(activity1,64206,
* -1,activity1.getIntent());
*/

if (session.isOpened()) {

List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
RegisterActivity.this, PERMISSIONS);
session.requestNewReadPermissions(newPermissionsRequest);
return;
}
// make request to the /me API
Request.newMeRequest(session, new GraphUserCallback() {

@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
Log.d("",""+user);
try {

name=user.getName();
email=user.getProperty("email").toString();
location=(user.getLocation().getProperty("name").toString());

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}).executeAsync();

}
}
});

isSubsetof method

private boolean isSubsetOf(Collection<String> subset,
Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}

put this code in onActivityResult

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

Log.d("Activity result"," "+requestCode+" "+resultCode+" "+data.toString());
Session.getActiveSession().onActivityResult(this, requestCode,
resultCode, data);
}

Put Internet permission in Manifest

   <uses-permission android:name="android.permission.INTERNET" />

Declare Facebook Login Activity in manifest


    <meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id" />

Facebook Android SDK 3.0 always returns OPENING when logging in

It wasn't easy to figure this out. :-)

If you actually check out exception in onSessionStateChange(), you'll see that it's not really simply stuck in OPENING. That exception contains: java.lang.UnsupportedOperationException: Session: an attempt was made to open a session that has a pending request.

The reason I finally found was that onActivityResult() wasn't called. It's its responsibility to call back to uiHelper and that call will call the finishing part of the login, so it if isn't called, the state stays OPENING forever. Apart from the obvious pieces of advice above, this is what I found and they are all needed for the login to work:

Make sure you use both a fragment and an activity, as described in the FB documentation. Even if the doc hints at an activity being enough, it isn't. You need both even if the activity has nothing else but calling the single fragment.

Make sure you have no history set for the activity (neither in the manifest nor in code).

Make sure you override the fragment's onActivityResult() as per the documentation. And make sure you override the same in the activity as well:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}

Yes, this seems strange but it doesn't work without it.



Related Topics



Leave a reply



Submit