401阅读推文时的回复

时间:2011-08-10 08:38:22

标签: java blackberry twitter

我是移动应用程序开发的新手。我正在开发一个黑莓应用程序,它从用户的时间线上读取推文。到目前为止,我设法获得了OAuth访问令牌。当我尝试使用此访问令牌来阅读推文时,问题就出现了,我得到了一条带有“未授权”消息的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();
        }
    }

1 个答案:

答案 0 :(得分:0)

我不是OAuth的专家,但只是一个注释:

此:

httpConn.setRequestMethod(HttpProtocolConstants.HTTP_METHOD_GET);

和此:

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

是相互排斥的东西。您正在将数据发布到服务器,因此它应该是POST(而不是GET)。