2010-07-08 26 views
3

我已經嘗試了很多不同的方式,並搜索整個互聯網找到一個教程,使用OAuth與JTwitter。以下是接下來的步驟我都做到簡單的JTwitter爲Android例子

同時下載Jtwitter和路標 添加他們作爲罐子在Java構建

創建一個簡單的按鈕運行

public class ShareGenerator extends Activity { 

    private static final String JTWITTER_OAUTH_KEY = "this_is_populated"; 
    private static final String JTWITTER_OAUTH_SECRET = "this_is_populated"; 

    Button menupopButton; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.share); 
    this.setContentView(R.layout.share); 
    this.txShare = (TextView)this.findViewById(R.id.lblshare); 
    this.menupopButton = (Button)this.findViewById(R.id.menupop); 


    menupopButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 

     { 
      TwitterSend(); 

     } 
}); 

    } 

,我有我的課

public void TwitterSend() { 

       OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob"); 
        Twitter jtwit = new Twitter("bob", client); 
        // open the authorisation page in the user's browser 
        client.authorizeDesktop(); 
        // get the pin 
        String v = client.askUser("Please enter the verification PIN from Twitter"); 
        client.setAuthorizationCode(v); 
        // Optional: store the authorisation token details 
        Object accessToken = client.getAccessToken(); 
        // use the API! 
        jtwit.setStatus("Messing about in Java"); 

      } 

但是我甚至無法彈出OAuth屏幕。它到達那裏時崩潰。任何人都可以幫助我至少看到OAuth屏幕?我的輸入設置正確。

+0

看看Logcat講述了什麼崩潰,並確保您的清單中有權限INTERNET。 – Pentium10 2010-07-08 08:47:59

+0

Logcat說java.lang.NoClassDefFoundErrors:java.awt.Desktop 它在client.authorizeDesktop()失敗; – JuniorFlip 2010-07-08 16:32:27

+0

我無法將URI轉換爲Uri以便在setData()方法中傳遞url。任何建議要這樣做? – 2011-02-03 04:17:12

回答

3

的問題是在這條線,它採用的java.awt.Desktop:

// open the authorisation page in the user's browser 
client.authorizeDesktop(); 

這將在臺式機上運行,​​而不是在Android上。

取而代之,從client.authorizeUrl();獲取網址並將用戶發送到那裏。例如。與這樣的事情:

URI url = client.authorizeUrl(); 
Intent myIntent = new Intent(Intent.VIEW_ACTION); 
myIntent.setData(url); 
startActivity(myIntent); 

但我不是一個Android編碼器!通過使用回調而不是oob,您幾乎可以肯定會做得更好。希望別人能提供的代碼爲...

-2

看到此信息[查看轉歷史,如果你不在乎]

+5

你會找到每一個相關的問題,並提供相同的答案,不是嗎? – Will 2010-11-17 15:24:48

1

丹尼爾提供了一個很好的起點。這就是我在Android應用中實現這一點:

OAuthSignpostClient authClient = new OAuthSignpostClient('apiKey','apiSecret','callbackUrl'); 

       java.net.URI jUrl = authClient.authorizeUrl(); 
       Uri.Builder uriBuilder = new Uri.Builder(); 
       uriBuilder.encodedPath(jUrl.toString()); 

       Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(jUrl.toString())); 
       startActivity(myIntent); 

當然,我用'apiKey'等贅述。您需要在構造函數OAuthSignpostClient中使用自己的密鑰,祕密和回調URL。

注:你需要通過JTwitter提供給Android.net.Uri的java.net.URI中的轉換爲使用它來啓動一個新的意圖。

在此之後,您將需要使用意圖過濾器來捕獲回調URL,並執行用戶令牌和祕密,您將從Twitter API獲得。

+0

我對你爲什麼使用uriBuilder這兩行你不使用有點困惑。 encodedPath有一個隱藏的結果還是我錯過了什麼? – Mikle 2012-01-17 03:11:44