2011-04-20 162 views
1

我可以使用此代碼與Twitter連接,當我將其作爲Java程序運行時,我可以獲取PIN。我可以找到這個引腳,但是我如何在BlackBerry模擬器中執行此操作?將Java SE代碼移植到黑莓

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import oauth.signpost.OAuth; 
import oauth.signpost.OAuthConsumer; 
import oauth.signpost.OAuthProvider; 
import oauth.signpost.basic.DefaultOAuthConsumer; 
import oauth.signpost.basic.DefaultOAuthProvider; 
import oauth.signpost.signature.SignatureMethod; 

public class Main { 

    public static void main(String[] args) throws Exception { 

     OAuthConsumer consumer = new DefaultOAuthConsumer(
       "my consumerkey", 
       "mysecret key here", 
       SignatureMethod.HMAC_SHA1); 

     OAuthProvider provider = new DefaultOAuthProvider(consumer, 
       "http://twitter.com/oauth/request_token", 
       "http://twitter.com/oauth/access_token", 
       "http://twitter.com/oauth/authorize"); 

     System.out.println("Fetching request token from Twitter..."); 

     // we do not support callbacks, thus pass OOB 
     String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND); 

     System.out.println("Request token: " + consumer.getToken()); 
     System.out.println("Token secret: " + consumer.getTokenSecret()); 

     System.out.println("Now visit:\n" + authUrl 
       + "\n... and grant this app authorization"); 
     System.out.println("Enter the PIN code and hit ENTER when you're done:"); 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
     String pin = br.readLine(); 

     System.out.println("Fetching access token from Twitter..."); 

     provider.retrieveAccessToken(pin); 

     System.out.println("Access token: " + consumer.getToken()); 
     System.out.println("Token secret: " + consumer.getTokenSecret()); 

     URL url = new URL("http://twitter.com/statuses/mentions.xml"); 
     HttpURLConnection request = (HttpURLConnection) url.openConnection(); 

     consumer.sign(request); 

     System.out.println("Sending request to Twitter..."); 
     request.connect(); 

     System.out.println("Response: " + request.getResponseCode() + " " 
       + request.getResponseMessage()); 
    } 
} 
+1

你有什麼問題嗎? – 2011-04-20 22:40:48

+0

我不知道如何使用這段代碼在模擬器中打開頁面。當我在java中這樣做時,我必須從控制檯複製鏈接,並且必須粘貼我在瀏覽器中的控制檯上的Url,然後瀏覽器將我帶到請求的URL並在那裏給我的用戶名和密碼並獲得並將其粘貼到控制檯上,並獲得訪問令牌和令牌的祕密,但如何在黑莓模擬器中執行類似的過程?而我必須在上面的代碼中做出什麼變化,它應該在黑莓模擬器中打開,我不知道??? plz幫助... – Sagar 2011-04-21 05:26:20

回答

1

爲了打開你要調用的黑莓瀏覽器:

Browser.getDefaultSession().displayPage("myUrl"); 

請記住,你的應用程序必須爲了做到這一點簽署。另外,我不知道這些OAuth類是否適用於Blackberry。