2

我在尝试获取通过 Facebook 登录的 Parse 用户的名称时遇到了很大的问题。

登录工作正常,我可以在解析数据浏览器上看到用户令牌, ParseUser.getCurrentUser 返回一个 ParseUser 和所有,但我不知道我可以从 facebook 获取用户名吗?

到目前为止我的代码是

ParseFacebookUtils.logIn(C_RegisterLogin.this, new LogInCallback()
{
    @Override
    public void done(ParseUser user, ParseException err)
    {
        progressdialog.dismiss();

        if (user == null)
        {
            Toast toast = Toast.makeText(getApplicationContext(), "Login via Facebook failed!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 0, 20);
            toast.show();
        }
        else
        {
            if (user.isNew())
            {
            }

            List args = new ArrayList<String>();
            args.add("name");
            JSONObject result = new JSONObject(ParseFacebookUtils.getSession().requestNewReadPermissions(C_RegisterLogin.this, args);
            String facebookname = result.optString("name");

            Toast toast = Toast.makeText(getApplicationContext(), "Thanks, "+facebookname+". You are now successfully logged in!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 0, 20);
            toast.show();
            finish();
            startActivity(new Intent(C_RegisterLogin.this, E_SelectJourney.class));
        }
    }
});
4

2 回答 2

3

解决了它,只需要它使用来自 facebook 的 Request.executeMeRequestAsync ,并且对于它的会话参数,我使用会话 Parse 给我:D

if (ParseFacebookUtils.getSession().isOpened())
{
    Request.executeMeRequestAsync(ParseFacebookUtils.getSession(), new Request.GraphUserCallback()
    {
        @Override
        public void onCompleted(GraphUser user, Response response)
        {
            if (user != null)
            {
                progressdialog.dismiss();
                Toast toast = Toast.makeText(getApplicationContext(), "Thanks, " + user.getName() + ". You are now successfully logged in through Facebook!", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.TOP, 0, 20);
                toast.show();
                finish();
                startActivity(new Intent(C_RegisterLogin.this, E_SelectJourney.class));
            }
        }
    });
}
于 2013-03-14T17:40:41.820 回答
0

Request.executeMeRequestAsync 方法已被弃用,因此对于 facebook sdk 3.21,您需要使用

   if (ParseFacebookUtils.getSession().isOpened()){
Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback()
{
    @Override
    public void onCompleted(GraphUser user, Response response)
    {
        if (user != null)
        {
            progressdialog.dismiss();
            Toast toast = Toast.makeText(getApplicationContext(), "Thanks, " + user.getName() + ". You are now successfully logged in through Facebook!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 0, 20);
            toast.show();
            finish();
            startActivity(new Intent(C_RegisterLogin.this, E_SelectJourney.class));
        }
    }
}).executeAsync();}
于 2015-03-12T15:36:33.163 回答