【发布时间】:2016-10-25 08:00:25
【问题描述】:
我正在尝试从已登录/已登录的 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("public_profile, email","user_friends"));更改您的读取权限 -
然后我在尝试登录时收到错误:无效范围:public_profile。在更改您的建议之前,我能够登录但无法获取个人资料名称,只有在启动我的应用程序之前在 facebook 应用程序上预先登录时
-
啊,也许你可以在更改阅读权限后试试我的回答
-
你确定 'loginButton.setReadPermissions(Collections.singletonList("public_profile, email","user_friends"));' 它说那里有错误。
-
啊等等,
loginButton.setReadPermissions(Collections.singletonList("public_profile, email,user_friends"));我改变了“位置”
标签: java android facebook android-fragments login