2012-01-31 66 views
1

在我的應用程序中,每次用戶試圖分享帖子時,都想顯示twitter登錄。Android中的Twitter身份驗證

我不想在共享首選項中保存Twitter憑據,但是當我第二次嘗試共享帖子時,它沒有顯示登錄字段。

任何人都可以找出問題嗎?

public class AuthorizationActivity extends Activity { 

private TwitterActivity app; 
private WebView webView; 
static String token, verifier; 

Webservice web; 
OAuthCredentialsResponse credentials; 
private WebViewClient webViewClient = new WebViewClient() { 
    @Override 
    public void onLoadResource(WebView view, String url) { 
     final OAuthHmacSigner signer = new OAuthHmacSigner(); 
     Uri uri = Uri.parse(url); 

     if (url.startsWith(Constants.OAUTH_CALLBACK_URL)) { 
      try { 

       if (url.indexOf("oauth_token=")!=-1) { 
      token = uri.getQueryParameter("oauth_token"); 
      verifier = uri.getQueryParameter("oauth_verifier"); 

      signer.clientSharedSecret = Constants.CONSUMER_SECRET; 

      OAuthGetAccessToken accessToken = new OAuthGetAccessToken(
        Constants.ACCESS_URL); 
      accessToken.transport = new ApacheHttpTransport(); 
      accessToken.temporaryToken = token; 
      accessToken.signer = signer; 
      accessToken.consumerKey = Constants.CONSUMER_KEY; 
      accessToken.verifier = verifier; 

      credentials = accessToken.execute(); 

      signer.tokenSharedSecret = credentials.tokenSecret; 


        + credentials.tokenSecret); 

      ; 
      if (null != token) { 
       webView.setVisibility(View.INVISIBLE); 


       finish(); 

      } else if (url.indexOf("error=")!=-1) { 
       view.setVisibility(View.INVISIBLE); 

      } 

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

    } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.authorization_view); 
    setUpViews(); 

String authURL = beginAuthorization(); 
webView.loadUrl(authURL); 
} 



private void setUpViews() { 
    webView = (WebView) findViewById(R.id.web_view); 
    webView.setWebViewClient(webViewClient); 
} 
public String beginAuthorization() { 
    try { 

     final OAuthHmacSigner signer = new OAuthHmacSigner(); 
     OAuthAuthorizeTemporaryTokenUrl authorizeUrl; 
     signer.clientSharedSecret = Constants.CONSUMER_SECRET; 

    OAuthGetTemporaryToken temporaryToken = new OAuthGetTemporaryToken(Constants.REQUEST_URL); 
     temporaryToken.transport = new ApacheHttpTransport(); 
     temporaryToken.signer = signer; 
     temporaryToken.consumerKey = Constants.CONSUMER_KEY; 
     temporaryToken.callback = Constants.OAUTH_CALLBACK_URL; 

     OAuthCredentialsResponse tempCredentials =      temporaryToken.execute(); 
     signer.tokenSharedSecret = tempCredentials.tokenSecret; 

    authorizeUrl = new OAuthAuthorizeTemporaryTokenUrl(Constants.AUTHORIZE_URL); 
     authorizeUrl.temporaryToken = tempCredentials.token; 
     String authorizationUrl = authorizeUrl.build(); 
     System.out.println(authorizationUrl); 

     return authorizationUrl; 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return null; 
} 

public void authorized() { 
    try { 



     AccessToken a = new AccessToken(credentials.token,credentials.tokenSecret); 
     Twitter twitter = new TwitterFactory().getInstance(); 
     twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET); 
     twitter.setOAuthAccessToken(a); 
     twitter.updateStatus("my first post"); 

     Toast t = Toast.makeText(this,"Successfully Shared",Toast.LENGTH_LONG); 
     t.show(); 

    } catch (TwitterException e) { 
     throw new RuntimeException("Unable to authorize user", e); 
    } 
} 
+1

我最好的猜測是,當您共享的第一個鳴叫你的應用程序中獲得的OAuth訪問令牌仍然是有效的,當你共享第二鳴叫(即尚未過期) 。我對OAuth沒有太多的工作,但我建議你看看如何「調整」令牌的到期時間,以便在共享推文後立即過期。請注意,如果您的OAuth庫在封面下使用刷新令牌,則這可能不起作用。 – curioustechizen 2012-01-31 07:24:04

回答

3

我想你錯過了在URL

force_login = true一個參數,這將要求用戶登錄,即使會話沒有過期。

要閱讀更多去這個鏈接RestAPI

+0

你可以指定如何把這個在「http://api.twitter.com/oauth/authorize」 – 2012-01-31 07:37:29

+0

現在我明白了..boss.thanks – 2012-01-31 07:44:56