Facebook SDK 3.7 for Android request email permission

拟墨画扇 提交于 2019-11-28 11:48:29

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" />

hey you can try this way...

session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("id", "name", "picture","installed" )).setCallback(statusCallback));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!