Android Facebook SDK 4.0 Login without Facebook App

前端 未结 4 1983
面向向阳花
面向向阳花 2020-12-18 21:32

I\'m having issues with the webview login for Facebook on Android.

I\'ve followed the tutorials and login works perfectly when the user has the Facebook app install

4条回答
  •  一向
    一向 (楼主)
    2020-12-18 22:12

    This is a working example without using Facebook app and if you close your app and open it again you'll be logged in automatically unless you log out. Here we get the user email address and friends list after log in.

    public class MainActivity extends Activity {
    
        private CallbackManager callbackManager;
        private LoginButton loginButton;
        private static final String TAG = "logTag";
        private TextView mFriend, mEmail;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            FacebookSdk.sdkInitialize(getApplicationContext());
            callbackManager = CallbackManager.Factory.create();
    
            setContentView(R.layout.activity_main);
            mFriend = (TextView) findViewById(R.id.tv_friend);
            mEmail = (TextView) findViewById(R.id.tv_email);
    
            loginButton = (LoginButton) findViewById(R.id.login_button);
            loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
            loginButton.registerCallback(callbackManager, new FacebookCallback() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d(TAG, "Successful Login");
                    GraphRequest friendsRequest = GraphRequest.newMyFriendsRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONArrayCallback() {
                                @Override
                                public void onCompleted(JSONArray objects, GraphResponse response) {
    
    
                                    String x = objects.opt(0).toString();
                                    mFriend.setText(x);
    
                                }
                            });
                    GraphRequest meRequest = GraphRequest.newMeRequest(
                            loginResult.getAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    try {
                                        String email = response.getJSONObject().getString("email").toString();
                                        mEmail.setText(email);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
    
                                }
                            });
    
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id, name, email, gender");
                    meRequest.setParameters(parameters);
                    meRequest.executeAsync();
    
                    parameters = new Bundle();
                    parameters.putString("field", "user_friends");
                    friendsRequest.setParameters(parameters);
                    friendsRequest.executeAsync();
    
                }
    
                @Override
                public void onCancel() {
                    Log.d(TAG, "Login Canceled");
                }
    
                @Override
                public void onError(FacebookException error) {
                    Log.d(TAG, "Login Error");
                }
            });
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
            super.onActivityResult(requestCode, resultCode, data);
            // manage login results
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    }
    

提交回复
热议问题