Android facebook授权。安装官方Facebook应用程序时无法登录

时间:2016-03-14 14:15:02

标签: android facebook authentication login

我需要登录Facebook并获取电子邮件,用户名,照片等字段。我使用Facebook SDK。当没有安装官方Facebook应用程序时,按下登录按钮,打开Web视图窗口,请求邮件和密码,然后,我可以获得令牌和数据。但是当在设备上安装官方Facebook应用程序时,我的应用程序无法正常工作,我按下登录按钮,但我没有看到对话框(Facebook活动没有启动)。我使用登录没有标准Facebook LogInButton。我怎么解决它?

    private CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(this);
            callbackManager = CallbackManager.Factory.create();
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(final LoginResult loginResult) {
                    GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
                            Bundle bFacebookData = getFacebookData(jsonObject);
                            new FacebookAuthorization(AuthenticationActivity.this, loginResult.getAccessToken().getToken(),
                                    bFacebookData.getString("name"),
                                    bFacebookData.getString("profile_pic"),
                                    bFacebookData.getString("email")).execute();
                        }
                    });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onError(FacebookException e) {

                }
            });
    }

    public void facebookAuth(View v){
            if (new InternetConnection(getApplicationContext()).isNetworkConnection()) {
                LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
            } else {
                Toast.makeText(getApplicationContext(), getResources().getString(R.string.no_internet_connection), Toast.LENGTH_SHORT).show();
            }
        }

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(callbackManager.onActivityResult(requestCode, resultCode, data)) {
            return;
        }
    }

    private Bundle getFacebookData(JSONObject object) {
            try {
                Bundle bundle = new Bundle();
                String id = object.getString("id");

                try {
                    URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=200&height=150");
                    bundle.putString("profile_pic", profile_pic.toString());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                }
                if (object.has("first_name") && object.has("last_name")){
                    bundle.putString("name", object.getString("first_name") + " " + object.getString("last_name"));
                }
                if (object.has("email")){
                    bundle.putString("email", object.getString("email"));
                }
                return bundle;
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

0 个答案:

没有答案