Android如何在使用Twitter进行身份验证后获取用户的全名

时间:2015-07-28 11:34:13

标签: android twitter

我按照以下步骤创建了Twitter登录http://docs.fabric.io/android/twitter/authentication.html,并显示除用户全名以外的用户名。

这是我的代码

loginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // Do something with result, which provides a TwitterSession for making API calls
                TwitterSession session = Twitter.getSessionManager().getActiveSession();
                TwitterAuthToken authToken = session.getAuthToken();
                String token = authToken.token;
                String secret = authToken.secret;

                long userID = result.data.getUserId();

                //This throws an Error Cannot find symbol showUser()
                User user = twitter.showUser(userID);

                //This throws error as well
                String fullname= user.getName();


                Log.d("login:", "twitter:success");
                Log.d("username", result.data.getUserName());
                Log.d("full name", fullname);

            }

            @Override
            public void failure(TwitterException exception) {
                // Do something on failure
            }
        });

请问我该如何获得用户的全名?

3 个答案:

答案 0 :(得分:3)

像这样使用TwitterApiClient和用户:

Twitter
    .getApiClient(session)
    .getAccountService()
    .verifyCredentials(true, false, new Callback<User>() {

        @Override
        public void failure(TwitterException e) {
        }

        @Override
        public void success(Result<User> userResult) {
            User user = userResult.data;
            .... access User fields ....
        }

    });

答案 1 :(得分:0)

您可以使用此功能获取用户名:

public void processToken(String callbackUrl) {
        mProgressDlg.setMessage("Finalizing ...");
        mProgressDlg.show();
        final String verifier = getVerifier(callbackUrl);
        new Thread() {
            @Override
            public void run() {
                int what = 1;

                try {
                    mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer,
                            verifier);

                    mAccessToken = new AccessToken(
                            mHttpOauthConsumer.getToken(),
                            mHttpOauthConsumer.getTokenSecret());

                    configureToken();

                    User user = mTwitter.verifyCredentials();

                    Log.i("user.getName()="+user.getName(), "786");
                    try{
                        firstName=user.getName().split(" ")[0];
                        lastName=user.getName().split(" ")[1];
                    }catch(Exception e){
                        e.printStackTrace();
                        firstName = user.getName();
                        lastName = "";
                    }
                    Log.i("firstName="+firstName+" lastName="+lastName, "786");
                    UserId=user.getId();

                    what = 0;
                } catch (Exception e) {
                    e.printStackTrace();
                }

                mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
            }
        }.start();
    }

答案 2 :(得分:0)

登录成功后,在您的public void success(Result<TwitterSession> result)方法中,使用此代码。

@Override
            public void success(Result<TwitterSession> result) {
                TwitterSession sess = TwitterCore.getInstance().getSessionManager().getActiveSession();

                TwitterApiClient apiClient = new TwitterApiClient(sess);
                final Call<com.twitter.sdk.android.core.models.User> getUserCall = apiClient
                        .getAccountService()
                        .verifyCredentials(true, false,true);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            com.twitter.sdk.android.core.models.User user = getUserCall.execute().body();
                            String realname = user.name;
                            long realid = user.id;
                            //similarly you can get user's other properties like location etc


                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }