我在获取通过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));
}
}
});
解决了它,只需要它使用来自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));
}
}
});
}
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();}