【问题标题】:Android facebook profle get nameAndroid facebook profile 获取名称
【发布时间】: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("pu‌​blic_profile, email","user_friends"));' 它说那里有错误。
  • 啊等等,loginButton.setReadPermissions(Collections.singletonList("pu‌​blic_profile, email,user_friends"));我改变了“位置”

标签: java android facebook android-fragments login


【解决方案1】:

也许您可以在将读取权限更改为 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();
        }

【讨论】:

    【解决方案2】:

    您有 Facebook 应用程序密钥吗? 您需要其中之一才能从您的应用程序访问 Facebook API,这可能解释了为什么除非您登录,否则您无法获得结果,该应用程序需要首先使用应用程序密钥对 Facebook API 进行身份验证。

    Link to Tutorial on how to get your key

    【讨论】:

    • 如果您指的是,我有一个 Applikations-ID 和密钥哈希?我已经非常彻底地遵循了 developer.facebook 教程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2016-09-10
    • 1970-01-01
    • 2015-10-20
    • 2011-08-02
    • 1970-01-01
    相关资源
    最近更新 更多