HTTP响应代码:411为URL:http://api.twitter.com/oauth/request_token

时间:2011-07-08 06:23:27

标签: java api rest twitter

修改:

参考:http://dev.twitter.com/pages/auth

我获得了signature_base_string,signature_key,oauth_signature以及与twitter教程相同的所有其他参数。

  1. signature_base_string POST&安培; HTTPS%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&安培; oauth_callback%3Dhttp%253A%252F%252Flocalhost%253A3005%252Fthe_dance%252Fprocess_callback%253Fservice_provider_id%253D11%26oauth_consumer_key%3DGDdmIQH6jhtmLUypg82g%26oauth_nonce%3DQP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk%26oauth_signature_method%3DHMAC- SHA1%26oauth_timestamp%3D1272323042%26oauth_version%3D1.0

  2. signature_key oauth_consumer_secret = “MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&安培;”

  3. oauth_signature 8wUi7m5HFQy76nowoCThusfgB + Q =
  4. 现在我请求http://api.twitter.com/oauth/request_token使用“POST”方法并获取响应代码411。 当我从我的Firefox请求它时,我使用servlet doGet()。 我在代码或错误中错过了什么?谁能帮帮我?

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String base_url="http://api.twitter.com/oauth/request_token";
        String oauth_callback="http://api.twitter.com/1/statuses/public_timeline.json";
        String oauth_consumer_key="GDdmIQH6jhtmLUypg82g";
        String oauth_consumer_secret="MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&";
        String oauth_signature_method="HMAC-SHA1";
    
    
        int oauth_timestamp=1272323042;
        String oauth_version="1.0";
        String oauth_nonce="QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk";
    
        String oauth_signature="";
    
        String signature_base_string = null;
        try {
            signature_base_string = "POST"
                    + "&"
                    + URLEncoder.encode(
                            "https://api.twitter.com/oauth/request_token",
                            "UTF-8")
                    + "&"
                    + URLEncoder
                            .encode("oauth_callback=http%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11&oauth_consumer_key=GDdmIQH6jhtmLUypg82g&oauth_nonce=QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272323042&oauth_version=1.0",
                                    "UTF-8");
            System.out.println(signature_base_string);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        try {
            Mac mac = Mac.getInstance("HmacSHA1");
            SecretKeySpec secret = new SecretKeySpec(
                    oauth_consumer_secret.getBytes(), "HmacSHA1");
            mac.init(secret);
            byte[] digest = mac.doFinal(signature_base_string.getBytes());
    
            BASE64Encoder encoder = new BASE64Encoder();
            oauth_signature=encoder.encode(digest);
            System.out.println(oauth_signature);
    
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    
        URL url=new URL(base_url);
    
        HttpURLConnection httpURLCon=(HttpURLConnection)url.openConnection(MyProxy.getProxy());
    
    
        httpURLCon.setRequestMethod("POST");
        httpURLCon.setRequestProperty("Authorization", "OAuth "+"oauth_nonce='QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', oauth_callback='http%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11', oauth_signature_method='HMAC-SHA1', oauth_timestamp='1272323042', oauth_consumer_key='GDdmIQH6jhtmLUypg82g', oauth_signature='8wUi7m5HFQy76nowoCThusfgB%2BQ%3D', oauth_version='1.0'");
        httpURLCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0");
    
    
        /*Map<String,List<String>> headers= httpURLCon.getHeaderFields();
    
        Iterator<String> i=headers.keySet().iterator();
        while(i.hasNext()){
            String key=i.next();
            System.out.println(key);
            System.out.println(headers.get(key).toString());
            System.out.println("====================");
        }
        */
    
        httpURLCon.connect();
        InputStream is=null;
        if (httpURLCon.getResponseCode() != 200) {
            is = httpURLCon.getErrorStream();
        } else {
            is = httpURLCon.getInputStream();
        }
    
        byte[] b=new byte[1024*4];
        int read;
        while((read=is.read(b))!=-1){
            response.getOutputStream().write(b,0,read);
        }
    }
    

    消息: 错误 无法检索请求的网址

    尝试处理请求时:

    POST / oauth / request_token HTTP / 1.1 主持人:api.twitter.com 授权:OAuth的oauth_nonce = 'QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk',oauth_callback = 'HTTP%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11',oauth_signature_method = 'HMAC-SHA1',oauth_timestamp = '1272323042',oauth_consumer_key = 'GDdmIQH6jhtmLUypg82g' ,oauth_signature ='8wUi7m5HFQy76nowoCThusfgB%2BQ%3D',oauth_version ='1.0' User-Agent:Mozilla / 5.0(Windows NT 6.1; rv:5.0)Gecko / 20100101 Firefox / 5.0 缓存控制:无缓存 Pragma:没有缓存 接受:text / html,image / gif,image / jpeg,*; q = .2, / ; Q = 0.2 X-D-Forwarder:是的 Max-Forwards:10

    遇到以下错误:

    Invalid Request 
    

    HTTP请求的某些方面无效。可能的问题:

    Missing or unknown request method
    Missing URL
    Missing HTTP Identifier (HTTP/1.0)
    Request is too large
    Content-Length missing for POST or PUT requests
    Illegal character in hostname; underscores are not allowed 
    

    您的缓存管理员是网站管理员。 Generated Fri,08 Jul 2011 06:41:34 GMT by google.com(squid / 2.7.STABLE6)

    =============================================== ===========

    参考:http://dev.twitter.com/pages/auth

    我获得了signature_base_string,signature_key,oauth_signature以及与twitter教程相同的所有其他参数。

    1. signature_base_string POST&安培; HTTPS%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&安培; oauth_callback%3Dhttp%253A%252F%252Flocalhost%253A3005%252Fthe_dance%252Fprocess_callback%253Fservice_provider_id%253D11%26oauth_consumer_key%3DGDdmIQH6jhtmLUypg82g%26oauth_nonce%3DQP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk%26oauth_signature_method%3DHMAC- SHA1%26oauth_timestamp%3D1272323042%26oauth_version%3D1.0

    2. signature_key oauth_consumer_secret = “MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&安培;”

    3. oauth_signature 8wUi7m5HFQy76nowoCThusfgB + Q =
    4. 现在我请求http://api.twitter.com/oauth/request_token使用“POST”方法并获取响应代码411。 当我从我的Firefox请求它时,我使用servlet doGet()。 我在代码或错误中错过了什么?谁能帮帮我?

          protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
          String base_url="http://api.twitter.com/oauth/request_token";
          String oauth_callback="http://api.twitter.com/1/statuses/public_timeline.json";
          String oauth_consumer_key="GDdmIQH6jhtmLUypg82g";
          String oauth_consumer_secret="MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&";
          String oauth_signature_method="HMAC-SHA1";
      
      
          int oauth_timestamp=1272323042;
          String oauth_version="1.0";
          String oauth_nonce="QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk";
      
          String oauth_signature="";
      
          String signature_base_string = null;
          try {
              signature_base_string = "POST"
                      + "&"
                      + URLEncoder.encode(
                              "https://api.twitter.com/oauth/request_token",
                              "UTF-8")
                      + "&"
                      + URLEncoder
                              .encode("oauth_callback=http%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11&oauth_consumer_key=GDdmIQH6jhtmLUypg82g&oauth_nonce=QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1272323042&oauth_version=1.0",
                                      "UTF-8");
              System.out.println(signature_base_string);
          } catch (UnsupportedEncodingException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      
          try {
              Mac mac = Mac.getInstance("HmacSHA1");
              SecretKeySpec secret = new SecretKeySpec(
                      oauth_consumer_secret.getBytes(), "HmacSHA1");
              mac.init(secret);
              byte[] digest = mac.doFinal(signature_base_string.getBytes());
      
              BASE64Encoder encoder = new BASE64Encoder();
              oauth_signature=encoder.encode(digest);
              System.out.println(oauth_signature);
      
          } catch (Exception e) {
              System.out.println(e.getMessage());
          }
      
          URL url=new URL(base_url);
      
          HttpURLConnection httpURLCon=(HttpURLConnection)url.openConnection(MyProxy.getProxy());
      
      
          httpURLCon.setRequestMethod("POST");
          httpURLCon.setRequestProperty("Authorization", "OAuth "+"oauth_nonce='QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk', oauth_callback='http%3A%2F%2Flocalhost%3A3005%2Fthe_dance%2Fprocess_callback%3Fservice_provider_id%3D11', oauth_signature_method='HMAC-SHA1', oauth_timestamp='1272323042', oauth_consumer_key='GDdmIQH6jhtmLUypg82g', oauth_signature='8wUi7m5HFQy76nowoCThusfgB%2BQ%3D', oauth_version='1.0'");
          httpURLCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0");
      
      
          /*Map<String,List<String>> headers= httpURLCon.getHeaderFields();
      
          Iterator<String> i=headers.keySet().iterator();
          while(i.hasNext()){
              String key=i.next();
              System.out.println(key);
              System.out.println(headers.get(key).toString());
              System.out.println("====================");
          }
          */
      
          httpURLCon.connect();
          InputStream is=httpURLCon.getInputStream();
          byte[] b=new byte[1024*4];
          int read;
          while((read=is.read(b))!=-1){
              response.getOutputStream().write(b,0,read);
          }
      }
      

      例外:

      java.io.IOException: Server returned HTTP response code: 411 for URL: http://api.twitter.com/oauth/request_token
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)
      at com.twitterbridge.oauth.Oauth.doGet(Oauth.java:114)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:619)
      

1 个答案:

答案 0 :(得分:0)

在我可以帮助您之前,您的HTTP错误411状态中的InputStream是什么?

添加以下代码。

InputStream is = null;
if (httpURLCon.getResponseCode() != 200) {
    is = httpURLCon.getErrorStream();
} else {
    is = httpURLCon.getInputStream();
}

更新:您的Authorization标头值必须从不包含空格。该值必须为OAuth<space><comma-delimited-value-without-space>