Facebook SDK 3.7 for Android request email permission

前端 未结 2 1676
一整个雨季
一整个雨季 2020-12-10 23:12

I am using Facebook SDK for Android to allow users to register and use app. I want to get some basic info about user like user name and email. Anyway, I have problem because

相关标签:
2条回答
  • 2020-12-10 23:33

    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" />
    
    0 讨论(0)
  • 2020-12-10 23:33

    hey you can try this way...

    session.openForRead(new Session.OpenRequest(this).setPermissions(Arrays.asList("id", "name", "picture","installed" )).setCallback(statusCallback));
    
    0 讨论(0)
提交回复
热议问题