您好我试图从Facebook获取基本的用户个人资料信息,例如firstname和lastname。我使用andriod作为我的客户端设备。但我无法检索用户的信息。我已经创建了我的appId,并在我的清单中添加了facebook的活动。用户对象只是一个包含用户名和姓的模型。我打算在另一个活动页面上显示信息。请有人能告诉我我做错了什么 下面是我的代码:
public class MainActivity extends AppCompatActivity {
private CallbackManager callbackManager;
private LoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//initialize facebook sdk
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
FaceBookSetup();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void FaceBookSetup() {
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton)findViewById(R.id.login_button);
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_first_name", "user_last_name"));
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Profile profile = Profile.getCurrentProfile();
User user = new User();
user.setFirstName(profile.getFirstName());
user.setLastName(profile.getLastName());
Log.d("First Name: ", user.getFirstName());
GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try {
/* User user = new User();
user.setFirstName(jsonObject.getString("first_name"));
user.setLastName(jsonObject.getString("last_name"));
Log.d("VISUAL: ", jsonObject.getString("first_name"));*/
}
catch(JSONException ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "first_name,last_name");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
});
}
//I also intend to pass users last and first name to another activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, requestCode, data);
}
}
my activity.xml:
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="148dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
答案 0 :(得分:0)
没有&#34; user_first_name&#34;和&#34; user_last_name&#34;。 Check可用权限列表。
我建议更改此行代码
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_first_name", "user_last_name"));
到此:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile"));
更改此行代码:
callbackManager.onActivityResult(requestCode, requestCode, data);
到此:
callbackManager.onActivityResult(requestCode, resultCode, data);