Facebook authentication without login button

后端 未结 6 1212
Happy的楠姐
Happy的楠姐 2020-11-28 21:33

I have followed some Facebook API 3.0 tutorials, including the Login/Logout and the Publish To Feed examples. So the login works this way:

  1. App opens, shows a f
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 21:58

    Something like this

    private void performFacebookLogin()
    {
        Log.d("FACEBOOK", "performFacebookLogin");
        final Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, Arrays.asList("email"));
        Session openActiveSession = Session.openActiveSession(this, true, new Session.StatusCallback()
        {
            @Override
            public void call(Session session, SessionState state, Exception exception)
            {
                Log.d("FACEBOOK", "call");
                if (session.isOpened() && !isFetching)
                {
                    Log.d("FACEBOOK", "if (session.isOpened() && !isFetching)");
                    isFetching = true;
                    session.requestNewReadPermissions(newPermissionsRequest);
                    Request getMe = Request.newMeRequest(session, new GraphUserCallback()
                    {
                        @Override
                        public void onCompleted(GraphUser user, Response response)
                        {
                            Log.d("FACEBOOK", "onCompleted");
                            if (user != null)
                            {
                                Log.d("FACEBOOK", "user != null");
                                org.json.JSONObject graphResponse = response.getGraphObject().getInnerJSONObject();
                                String email = graphResponse.optString("email");
                                String id = graphResponse.optString("id");
                                String facebookName = user.getUsername();
                                if (email == null || email.length() < 0)
                                {
                                    Logic.showAlert(
                                            ActivityLogin.this,
                                            "Facebook Login",
                                            "An email address is required for your account, we could not find an email associated with this Facebook account. Please associate a email with this account or login the oldskool way.");
                                    return;
                                }
                            }
                        }
                    });
                    getMe.executeAsync();
                }
                else
                {
                    if (!session.isOpened())
                        Log.d("FACEBOOK", "!session.isOpened()");
                    else
                        Log.d("FACEBOOK", "isFetching");
    
                }
            }
        });
    

    Actually exactly like that. It works perfectly fine for me.

提交回复
热议问题