Android Twitter Oauth获取屏幕名称

时间:2012-11-17 02:53:41

标签: android twitter oauth twitter-oauth

我想知道在使用android中的oauth登录到Twitter后我的屏幕名称。

@SuppressLint("NewApi")
    private void Authorize()
    {
        try {
            Consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
            Provider = new CommonsHttpOAuthProvider("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize");
            StrictMode.enableDefaults();
            String authUrl = Provider.retrieveRequestToken(Consumer, CALLBACK_URL);
            Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }


@Override
    protected void onResume() {
        super.onResume();
        try
        {           
            Uri uri = this.getIntent().getData();  
            if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
                String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
                Consumer.setTokenWithSecret(Consumer.getToken(),Consumer.getTokenSecret());
                Provider.retrieveAccessToken(Consumer, oauth_verifier);
                HttpParameters params1 = Provider.getResponseParameters();
                String userName = params1.getFirst("screen_name");
                Toast.makeText(this, "user name >>>>>>>>" + userName, Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

抛出exResume函数,但e.getMessage()为null  ( Provider.retrieveAccessToken(Consumer,oauth_verifier); 错误行)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你应该做params1.get(“screen_name”)。getFirst();

像这样

SortedSet<String> screenNameSet = mProvider.getResponseParameters().get("screen_name");  
String screenName = screenNameSet.first();