Android Kotlin-Facebook登录-获取用户数据

时间:2018-08-07 22:09:29

标签: android kotlin facebook-sdk-4.0

我是android开发的新手,已经搜索了数小时,却没有找到Facebook登录后如何获取该死用户数据的方法!

这是代码:

private var callbackManager: CallbackManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_fb_connect)




        // Login
        callbackManager = CallbackManager.Factory.create()
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"))
        LoginManager.getInstance().registerCallback(callbackManager,
                object : FacebookCallback<LoginResult> {
                    override fun onSuccess(loginResult: LoginResult) {
                        Log.d("letsSee", "Facebook token: " + loginResult.accessToken.token)

                    }

                    override fun onCancel() {
                        Log.d("letsSee", "Facebook onCancel.")

                    }

                    override fun onError(error: FacebookException) {
                        Log.d("letsSee", "Facebook onError.")

                    }
                })

}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    callbackManager?.onActivityResult(requestCode, resultCode, data)

    Log.d("letsSee", "malsehnnnnnn: " + data)
}

底部的Log.d("letsSee", "malsehnnnnnn: " + data)给出:malsehnnnnnn: Intent { (has extras) }

该死的数据在哪里???? Facebook文档很奇怪而且很糟糕!

1 个答案:

答案 0 :(得分:3)

发送GraphRequest usign accessToken,您将获得请求字段的json。

已更新为Kotlin

LoginManager.getInstance().registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
        override fun onSuccess(loginResult: LoginResult) {
            Logger.d("FBLOGIN", loginResult.accessToken.token.toString())
            Logger.d("FBLOGIN", loginResult.recentlyDeniedPermissions.toString())
            Logger.d("FBLOGIN", loginResult.recentlyGrantedPermissions.toString())


            val request = GraphRequest.newMeRequest(loginResult.accessToken) { `object`, response ->
                try {
                    //here is the data that you want
                    Logger.d("FBLOGIN_JSON_RES", `object`.toString())

                    if (`object`.has("id")) {
                        handleSignInResultFacebook(`object`)
                    } else {
                        Logger.e("FBLOGIN_FAILD", `object`.toString())
                    }

                } catch (e: Exception) {
                    e.printStackTrace()
                    dismissDialogLogin()
                }
            }

            val parameters = Bundle()
            parameters.putString("fields", "name,email,id,picture.type(large)")
            request.parameters = parameters
            request.executeAsync()

        }

        override fun onCancel() {
            Logger.e("FBLOGIN_FAILD", "Cancel")
        }

        override fun onError(error: FacebookException) {
            Logger.e("FBLOGIN_FAILD", "ERROR", error)
        }
    })

旧答案

这是为我工作的Java代码

    LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Logger.d("FBLOGIN", String.valueOf(loginResult.getAccessToken().getToken()));
            Logger.d("FBLOGIN", String.valueOf(loginResult.getRecentlyDeniedPermissions().toString()));
            Logger.d("FBLOGIN", String.valueOf(loginResult.getRecentlyGrantedPermissions().toString()));


            final GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) {
                    try {
                        //here is the data that you want
                        Logger.d("FBLOGIN_JSON_RES", String.valueOf(object));

                        if (object.has("id")) {
                            handleSignInResultFacebook(object);
                        } else {
                            Logger.e("FBLOGIN_FAILD", String.valueOf(object));
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                        dismissDialogLogin();
                    }
                }
            });

            final Bundle parameters = new Bundle();
            parameters.putString("fields", "name,email,id,picture.type(large)");
            request.setParameters(parameters);
            request.executeAsync();

        }

        @Override
        public void onCancel() {
            Logger.e("FBLOGIN_FAILD", "Cancel");
        }

        @Override
        public void onError(FacebookException error) {
            Logger.e("FBLOGIN_FAILD", "ERROR", error);
        }
    });

转换为kotlin