Android - Facebook和谷歌登录

时间:2016-12-11 05:10:35

标签: java android facebook google-signin

我正在尝试处理Facebook和Google登录到单个活动

谷歌登录有效,但Facebook登录没有显示请求权限对话框。当我点击使用Facebook按钮登录时,我希望看到公共个人资料和电子邮件的权限对话框。虽然对话框没有显示,但似乎我已登录,因为登录Facebook更改为Logout。有没有更好的方法呢?

    var checkedNum = $('input[name="model[]"]:selected').length;
    alert(checkedNum);
    if (checkedNum > 1) {
        alert('Validating the form2');
        // User didn't check any checkboxes
    } else {
        die();
    }

2 个答案:

答案 0 :(得分:0)

试试这个: 声明变量:

  private CallbackManager callbackManager;
  private AccessTokenTracker accessTokenTracker;
  com.facebook.Profile profile;
  private ProfileTracker mProfileTracker;
你的onCreate()方法中的

..

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    updateWithToken(AccessToken.getCurrentAccessToken());
    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
            updateWithToken(newToken);
        }
    };
    accessTokenTracker.startTracking();

现在在updateWithToken()方法

 private void updateWithToken(AccessToken currentAccessToken) {

    if (currentAccessToken != null) {
        LoginManager.getInstance().logOut();
    } else {
    }
}

现在在回调管理器中,您必须跟踪用户的当前个人资料

List<String> permissionNeeds = Arrays.asList(
            "public_profile", "email", "user_birthday", "user_friends");
    loginButton.setReadPermissions(permissionNeeds);

    loginButton.registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d("Success", "Login");
                    try {
                        if (Profile.getCurrentProfile() == null) {
                            mProfileTracker = new ProfileTracker() {
                                @Override
                                protected void onCurrentProfileChanged(Profile profile_old, Profile profile_new) {
                                    // profile2 is the new profile
                                    profile = profile_new;
                                    if (profile_new != null)
                                        Log.v("facebook - profile", profile_new.getFirstName());
                                    mProfileTracker.stopTracking();
                                }
                            };
                            mProfileTracker.startTracking();

                        } else {
                            profile = Profile.getCurrentProfile();
                            if (profile != null)
                                Log.v("facebook - profile", profile.getFirstName());
                        }

                        GraphRequest request = GraphRequest.newMeRequest(
                                loginResult.getAccessToken(),
                                new GraphRequest.GraphJSONObjectCallback() {
                                    @Override
                                    public void onCompleted(JSONObject object, GraphResponse response) {
                                        // Application code
                                        try {
                                            Log.v("FACEBOOK LOGIN", response.toString());
                                            fb_id = object.getString("id");
                                            fb_name = object.getString("name");
                                            fb_gender = object.getString("gender");
                                            fb_email = object.getString("email");
                                            fb_birthday = object.getString("birthday");
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                            Log.d("Error", e.toString());
                                    }
                                });
                        Bundle parameters = new Bundle();
                        parameters.putString("fields", "id,name,email,gender,birthday,picture.type(small)");
                        request.setParameters(parameters);
                        request.executeAsync();

                    } catch (Exception e) {
                        Log.d("ERROR", e.toString());
                    }
                }

                @Override
                public void onCancel() {
                    Log.d("FACEBOOK ERRROR", "cancelled");
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d("FACEBOOK ERRROR", exception.toString());
            });

答案 1 :(得分:0)

如果您不介意使用WebView,请尝试使用CloudRail进行社交登录,使用起来非常简单。 https://github.com/CloudRail/cloudrail-si-android-sdk