2011-07-22 32 views
5

你好我有困難使用JTwitter函數來驗證我的twitter應用程序。我總是得到一個「TwitterException」Android Jtwitter,身份驗證問題

這裏是我的方法

OAuthSignpostClient oauthClient = new OAuthSignpostClient(consumerKey, 
      privateKey, "oob"); 

一)我不知道「OOB」值應該是什麼,它是「callbackURL」在我的Twitter應用它說"callBack URL: none"所以我試圖把"none","None"null其中"oob"沒有不同的結果。

那麼剩下的就是樣板

Intent i = new Intent(Intent.ACTION_VIEW, 
    Uri.parse(oauthClient.authorizeUrl().toString())); 
    startActivity(i); 
    // get the pin 
    String v = oauthClient.askUser("Please enter the verification PIN from Twitter"); 
    oauthClient.setAuthorizationCode(v); 
    // Store the authorisation token details for future use 
    String[] accessToken = oauthClient.getAccessToken(); 
    // Next time we can use new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET, 
    //  accessToken[0], accessToken[1]) to avoid authenticating again. 

    EditText twitterText = (EditText)findViewById(R.id.twitterText); 
    twitterText.getText(); 

    // Make a Twitter object 
    Twitter twitter = new Twitter(null, oauthClient); 
    // Print Daniel Winterstein's status 
    //System.out.println(twitter.getStatus("winterstein")); 
    // Set my status 
    twitter.setStatus(twitterText.getText()); 

在這一點上,我根本不知道如何使這項工作。希望我可以更詳細地瞭解它,但它與認證有關。我看到的在線事物沒有幫助

回答

7

試試這個爲您的回調網址

OAuthSignpostClient oauthClient = 
    new OAuthSignpostClient(consumerKey, privateKey, "callback://twitter"); 

切記!後:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 

執行以下操作

覆蓋onResume()得到驗證碼

protected void onResume() { 
    super.onResume(); 
    if (this.getIntent()!=null && this.getIntent().getData()!=null){ 
     Uri uri = this.getIntent().getData(); 
     if (uri != null && uri.toString().startsWith("callback://twitter")) { 
      //Do whatever you want 
      //usually use uri.getQueryParameter(someString); 
      //test what could be someString using Log.v("",uri.toString()); 
      //u want the Authorization code which is a couple of numbers 
      //so you could use oauthClient.setAuthorizationCode(v); 
      //and finally initialise Twitter 
     } 
    } 
} 

您可以使用uri.getQueryParameterNames()來獲取參數String名。


@BobVork

我只是想補充一點,你需要有一個回調URL在設置選項卡中設置你的Twitter的應用程序(在twitter.com上)。

它甚至不在乎您在該字段中輸入的內容,但如果它爲空,則回調網址將無法工作。

+1

我只想補充一點,您需要在Twitter應用的設置標籤中設置回調網址(在twitter.com上)。它甚至不關心你在該領域投入什麼,但如果它是空的,回調網址將無法工作。這花了我大約一個小時才發現,所以我認爲這可能會讓別人節省一些時間。 –

1

我不確定這是問題所在,但好像你從未設置twitter對象的用戶名。

+0

我認爲誓言的目的是讓用戶不必把他們的用戶名或其他東西...更難代碼呢?這個概念有點混淆,我的第一個直覺是用戶名在某處,但是這個twitter對象似乎並不需要它,並且有人宣誓會處理它 – CQM

+0

我不是那麼熟悉twitter API,但是我在任何地方看(甚至在您從代碼中獲取代碼的示例),即使在使用oAuth時也使用了用戶名。您可以保留一次,然後在需要時使用它,您不必每次都從某些TextView中讀取它。 – MByD

1

嗯,我不知道它是如何與Jtwitter API,我不知道這個點足夠的Java真正理解此代碼的內部運作,但你有沒有試過,而不是NULLNone,代替嘗試?。這是從Javascript進行調用時用於restful API的回調。試試看。