gpt4 book ai didi

java - Android facebook 个人资料获取姓名

转载 作者:行者123 更新时间:2023-12-01 09:19:54 26 4
gpt4 key购买 nike

我正在尝试从已登录/已登录的 facebook 用户检索个人资料名称。如果我在运行应用程序之前已经登录 facebook,它只会让我检索该名称,否则它将是无效的。我正在尝试检索名称并将其显示在 TextView 对象中。

public class MainFragment extends Fragment {

private TextView mTextDetails;
private CallbackManager mCallbackManager;
private FacebookCallback<LoginResult> mCallback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) { // The thing you want to do when you got permission
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
if(profile != null){
mTextDetails.setText("Welcome " + profile.getName());
}
}

@Override
public void onCancel() {

}

@Override
public void onError(FacebookException error) {

}
};

public MainFragment() {
// Required empty public constructor
}

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
mCallbackManager = CallbackManager.Factory.create();

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
mTextDetails = (TextView)view.findViewById(R.id.text_details);
LoginButton loginButton = (LoginButton)view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");//Only ask if you must
loginButton.setFragment(this);
loginButton.registerCallback(mCallbackManager, mCallback);


return view;
}

public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode,resultCode, data);

}

}

最佳答案

也许您可以在将读取权限更改为 loginButton.setReadPermissions(Collections.singletonList("pu‌​blic_profile, email")); 后使用 GraphRequest 获取配置文件名称; graphrequest 为

@Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {

try {
if (object != null) {
String name = object.getString("first_name") + " "+object.getString("last_name");
}
} catch (JSONException e) {

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

关于java - Android facebook 个人资料获取姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40234471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com