2011-08-10 72 views
0

我是新手機應用程序開發人員。我正在開發一個黑莓應用程序,它從用戶的時間線上讀取推文。到目前爲止,我設法獲得了OAuth訪問令牌。當我嘗試使用此訪問令牌讀取推文時,出現問題,我收到帶有「未經授權」消息的401響應。我沒有使用任何庫,我正在自己做所有事情。任何人都可以幫助我嗎?閱讀推文時的401響應

感謝,

下面的代碼:

  HttpConnectionFactory factory = new HttpConnectionFactory(url, 
       HttpConnectionFactory.TRANSPORT_WIFI | 
       HttpConnectionFactory.TRANSPORT_WAP2 | 
       HttpConnectionFactory.TRANSPORT_BIS | 
       HttpConnectionFactory.TRANSPORT_BES | 
       HttpConnectionFactory.TRANSPORT_DIRECT_TCP); 

     httpConn = factory.getNextConnection(); 
     httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET); 
     httpConn.setRequestProperty("WWW-Authenticate","OAuth realm=http://twitter.com/"); 
     httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     httpConn.setRequestProperty("Content-Length", Integer.toString(header.getBytes().length)); 
     os = httpConn.openOutputStream(); 

     os.write(header.getBytes()); 
     os.close(); 
     os = null; 


     input = httpConn.openDataInputStream(); 
     int resp = httpConn.getResponseCode(); 

    // Dialog.alert(httpConn.getDate()+" : "+System.currentTimeMillis()); 


     if (resp == HttpConnection.HTTP_OK) { 

      XMLReader parser; 
      try { 
       parser = XMLReaderFactory.createXMLReader(); 
       parser.setContentHandler(this); 
       parser.parse(new InputSource(input)); 
       for(int i=0 ; i<2 ; i++) 
       { 
        tweets.addElement(parser.getProperty("text").toString()); 
        Dialog.alert(parser.getProperty("text").toString()); 
       } 

      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Dialog.alert("your tweet was posted successfully :)"); 
     } 
     Dialog.alert(httpConn.getResponseCode()+": "+httpConn.getResponseMessage()); 
     return (httpConn.getResponseCode()+": "+httpConn.getResponseMessage()); 
    } catch (IOException e) { 
     return "exception"; 
    } catch (NoMoreTransportsException nc) { 
     return "noConnection"; 
    } finally { 
     try { 
      httpConn.close(); 
      input.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

請發佈您的代碼。 – Jacob

+0

我收錄了代碼 – Alia

+0

爲什麼不嘗試使用lib?這將大大簡化您的工作。我在我的BB應用中使用了這一個http://twitter4j.org/en/index.html – 8vius

回答

0

我不是OAuth的專家,不過只是一張紙條:

此:

httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET); 

這:

httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

是互斥的東西。您將數據發佈到服務器,所以它應該是一個POST(而不是GET)。