繁体   English   中英

使用Facebook登录到我的应用程序:使用Facebook App时出错

[英]Login to my app using facebook : error when using Facebook App

这是屏幕截图,使用Facebook凭据登录到我的应用程序无法通过浏览器正常工作。 但是通过Facebook App登录时会出现错误。 它不会自动重定向到我的应用程序,而是显示“您无法立即显示您请求的页面。它可能暂时不可用.....”。 提前致谢。 这是我的LoginActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_log);
        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");
        mail = (EditText) findViewById(R.id.log_mail);
        pass = (EditText) findViewById(R.id.log_pass);
        callbackManager = CallbackManager.Factory.create();


        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(
                    AccessToken oldAccessToken,
                    AccessToken currentAccessToken) {
                // Set the access token using
                // currentAccessToken when it's loaded or set.
            }
        };
        // If the access token is available already assign it.
        accessToken = AccessToken.getCurrentAccessToken();

        AppEventsLogger.activateApp(this);
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                Toast.makeText(getApplicationContext(), "Logged in with fb", Toast.LENGTH_SHORT).show();

                System.out.println("onSuccess");


                String accessToken = loginResult.getAccessToken().getToken();
                Log.i("", accessToken);

                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {

                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        Log.i("LoginActivity", response.toString());
                        // Get facebook data from login
                        Bundle bFacebookData = getFacebookData(object);
                        mail_id= bFacebookData.getString("email");
                        prof_pic= bFacebookData.getString("profile_pic");
                        f_name=bFacebookData.getString("first_name");
                        l_name=bFacebookData.getString("last_name");
                        bFacebookData.putString("img",prof_pic);
                        bFacebookData.putString("mail_id",mail_id);
                        bFacebookData.putString("fname",f_name);
                        bFacebookData.putString("lname",l_name);


                        Intent i = new Intent(LogActivity.this, HomeActivity.class);
//
                            i.putExtras(bFacebookData);
                            startActivity(i);
                        //
                        Toast.makeText(getApplicationContext(),mail_id,Toast.LENGTH_LONG).show();



                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // Parámetros que pedimos a facebook
                request.setParameters(parameters);
                request.executeAsync();

            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(), "Log in Cancelled..!!", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onError(FacebookException error) {
                Toast.makeText(getApplicationContext(), "Error in log in,error is : " + error, Toast.LENGTH_SHORT).show();

            }

        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        accessTokenTracker.stopTracking();
    }

    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");
                Log.i("profile_pic", profile_pic + "");
                bundle.putString("profile_pic", profile_pic.toString());

            } catch (MalformedURLException e) {
                e.printStackTrace();
                return null;
            }

            bundle.putString("idFacebook", id);
            if (object.has("first_name"))
                bundle.putString("first_name", object.getString("first_name"));
            if (object.has("last_name"))
                bundle.putString("last_name", object.getString("last_name"));
            if (object.has("email"))
                bundle.putString("email", object.getString("email"));
            if (object.has("gender"))
                bundle.putString("gender", object.getString("gender"));
            if (object.has("birthday"))
                bundle.putString("birthday", object.getString("birthday"));
            if (object.has("location"))
                bundle.putString("location", object.getJSONObject("location").getString("name"));
            return bundle;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

在基于模块的gradle文件中添加以下依赖项

compile 'com.facebook.android:facebook-android-sdk:4.7.0'

在setContentView之前初始化facebook sdk

FacebookSdk.sdkInitialize(YourActivity.this);

在onCreate中添加以下代码:

  callbackManager = CallbackManager.Factory.create();
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                if (CommonConstants.mDebug) Log.v(TAG, "onSuccess");

                final String accessToken = loginResult.getAccessToken().getToken();
                if (CommonConstants.mDebug) Log.v(TAG, accessToken);
                GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {

                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,first_name,last_name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                if (CommonConstants.mDebug) Log.v(TAG, "onCancel");
                if (ViewDialog.kProgressHUD.isShowing()) {
                    ViewDialog.hideProgress();
                }
            }

            @Override
            public void onError(FacebookException error) {
                if (CommonConstants.mDebug) Log.v(TAG, error.toString());
            }
        });

在onActivityResult中添加以下内容:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }

因为我使用的是最新版本的android-facebook-sdk,所以我已经更新了手机中的facebook应用程序,此问题得以解决。感谢大家的支持。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM