Modified:
Reference :http://dev.twitter.com/pages/auth
I have get signature_base_string,signature_key,oauth_signature and all other parameters that are the same with twitter tutorial.
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
signature_key oauth_consumer_secret="MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&"
- oauth_signature 8wUi7m5HFQy76nowoCThusfgB+Q=
Now I request http://api.twitter.com/oauth/request_token use "POST" method and get response code 411. I use a servlet doGet() when I request it from my Firefox. What do I miss in the code or any mistake? Who can help me?
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);
}
}
Message: ERROR The requested URL could not be retrieved
While trying to process the request:
POST /oauth/request_token HTTP/1.1 Host: api.twitter.com 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' User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0 Cache-Control: no-cache Pragma: no-cache Accept: text/html, image/gif, image/jpeg, *; q=.2, /; q=.2 X-D-Forwarder: yes Max-Forwards: 10
The following error was encountered:
Invalid Request
Some aspect of the HTTP Request is invalid. Possible problems:
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
Your cache administrator is webmaster. Generated Fri, 08 Jul 2011 06:41:34 GMT by google.com (squid/2.7.STABLE6)
==========================================================
Reference :http://dev.twitter.com/pages/auth
I have get signature_base_string,signature_key,oauth_signature and all other parameters that are the same with twitter tutorial.
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
signature_key oauth_consumer_secret="MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&"
- oauth_signature 8wUi7m5HFQy76nowoCThusfgB+Q=
Now I request http://api.twitter.com/oauth/request_token use "POST" method and get response code 411. I use a servlet doGet() when I request it from my Firefox. What do I miss in the code or any mistake? Who can help me?
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);
}
}
Exception:
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)
oauth_timestampis extremely old and will never validate. - Buhake Sindi