2011-11-29 205 views
6

與Twitter登錄我曾嘗試:安卓:使用Twitter4J

我已經註冊的Twitter應用程序,並得到了消費者的關鍵和Secret.Even我有不同的代碼與twitter.These登錄是什麼我從曾嘗試:

http://thetechnib.blogspot.com/2011/01/android-sign-in-with-twitter.html

[這個鏈接是死的,你可以查看檔案here]
http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application

問題,我有:

截至目前,上述代碼帶我到Twitter的登錄,讓我登錄,並有一個PIN碼來完成登錄process.But我不知道如何使用它來獲得我的應用程序working.I檢查了整個代碼,但沒有發現任何與pin相關的內容。

其次,當我註冊了twitter上我的應用程序,它要求回調URL,但因爲它是寫的,它真的不需要,我跳過指定。(即使我不知道它應該是什麼!)

因此,我在我的應用程序中將CallbackURL設爲null。

任何人都可以推薦我,我怎樣才能使用這個PIN來完成登錄過程並讓用戶回到我的應用程序的主要活動?它是導致問題或我做錯了什麼的回調網址?

請回復。任何幫助appriciated!謝謝。

編輯:

如弗蘭肯斯坦的建議,我試圖在代碼github.com/ddewaele/AndroidTwitterSample/downloads

我說我的消費者密鑰和消費者祕密與回調URL一起:

public static final String OAUTH_CALLBACK_SCHEME= "x-oauthflow-twitter"; 
public static final String OAUTH_CALLBACK_HOST= "callback"; 
public static final String OAUTH_CALLBACK_URL= OAUTH_CALLBACK_SCHEME + "://" + OAUTH_CALLBACK_HOST; 

,但它給了我這個錯誤:

的logcat:

11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): Error during OAUth retrieve request token 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match. 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:239) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:189) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at com.ecs.android.sample.twitter.OAuthRequestTokenTask.doInBackground(OAuthRequestTokenTask.java:55) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at com.ecs.android.sample.twitter.OAuthRequestTokenTask.doInBackground(OAuthRequestTokenTask.java:1) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at android.os.AsyncTask$2.call(AsyncTask.java:185) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 
11-29 11:56:56.249: E/com.ecs.android.sample.twitter.OAuthRequestTokenTask(3081): at java.lang.Thread.run(Thread.java:1096) 

還顯示我黑屏時,當我點擊TWEET按鈕,而不是帶我去登錄Twitter的屏幕。

Omg,我要瘋了......兩天以來一直在嘗試! :(請幫助

+0

您是否已經在處理授權過程的返回並存儲訪問令牌? – jmcdale

+0

嗯......我不知道它是否可以在代碼中節省代幣,因爲我是OAuth的新手。但看代碼,我不認爲這個令牌被保存了。 – Hiral

回答

9

這是因爲您的應用程序已註冊爲桌面客戶端。 要覆蓋回調URL,您的應用程序需要註冊爲瀏覽器客戶端。

嘗試 https://dev.twitter.com/apps/[appid]/settings>回調URL 和你的應用程序配置一個虛擬回調URL(http://example.com/或任何你想要的)將被視爲瀏覽器客戶端。

然後嘗試@Frankenstein或@ jamn224代碼。

+0

救了我累人的時刻......謝謝 –

5

你應該在明顯地寫回調,因爲這對你的活動

<activity android:name="com.apps.twitter.PrepareRequestTokenActivity" 
      android:launchMode="singleTask" android:theme="@android:style/Theme.Translucent.NoTitleBar" 
      android:screenOrientation="portrait"> 
      <intent-filter> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
       <data android:scheme="x-oauthflow-twitter" android:host="callback" /> 
      </intent-filter> 
</activity> 

,將恆流文件

final public static String CALLBACK_SCHEME = "x-oauthflow-twitter";  
final public static String CALLBACK_URL = CALLBACK_SCHEME + "://callback"; 
+0

我用我的問題的第二參考鏈接中指定的代碼嘗試了這一點。但不幸的是,它不適用於我。 :(無論如何,謝謝。 – Hiral

+0

也給「」作爲CallbackURL讓我登錄與Twitter但只是我卡在是,我不知道如何處理PIN我完成登錄過程。 – Hiral

+0

感謝您的我會一直試着讓你知道 – Hiral

7

首先,你需要正確驗證。

try{ 
     consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
     provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token", "http://twitter.com/oauth/access_token", "http://twitter.com/oauth/authorize"); 
     String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); 

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

    }catch(Exception e){ 
     Log.e(TAG,e+""); 
    } 

CALLBACK_URL的必要性需要在Manifest文件中設置(參考Frankenste在答案中)。上面的代碼開始在Twitter的服務器上執行授權的意圖。回調信息是必要的,所以意圖知道在授權過程之後要返回什麼應用程序。

然後我們需要在T​​witter的認證後,辦理退換貨的應用:

@Override 
public void onResume(){ 
    super.onResume(); 

    if (this.getIntent()!=null && this.getIntent().getData()!=null){ 
     Uri uri = this.getIntent().getData(); 

     //handle returning from authenticating the user 
     if (uri != null && uri.toString().startsWith(CALLBACK_URL)) { 
      String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); 
      String token = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_TOKEN); 
      try { 
       Twitter t = new TwitterFactory().getInstance(); 
       t.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 

       // Get Access Token and store it 
       rToken = new RequestToken(token, CONSUMER_SECRET); 
       AccessToken aToken = t.getOAuthAccessToken(rToken); 
       storeAccessToken(aToken); 

       //send to checkLoginState again since we have authorization now! 
       checkLoginState(); 

      } catch (Exception e) { 
       Log.e(TAG, e+""); 
      } 
      } 
     } 
}//end onResume 

此代碼從返回意圖抓住的數據,其中之一是搶授權令牌的信息。 「storeAccessToken(aToken)」是我寫的一個簡短方法,它將令牌存儲在應用程序的首選項中,以便我們不必在每次打開應用程序時重新授權。

現在,我們有一個授權令牌,我們可以用它授權了Twitter的實例:

twitter = new TwitterFactory().getInstance(); 
     twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
     twitter.setOAuthAccessToken(aToken); 

上述變量「推特」現在被授權,可以做它的事。

+0

感謝你的回覆,我會仔細研究一下 – Hiral

+0

糟糕...一個重要的事情是,我正在使用開源路標庫來協助oAuth程序。我使用公用庫和核心庫,這兩者都可以在[這裏]找到(http://code.google.com/p/oauth-signpost/downloads/list) – jmcdale

+0

你能告訴我如何存儲令牌?我的意思是,你可以給我代碼的方法storeAccessToken(aToken)和checkLoginState()以及 – Hiral