我得到一个异常和代码状态为 400。我的代码有什么问题?
OAuthConsumer consumer = new DefaultOAuthConsumer("API key","Secret",SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider(consumer,"http://services.digg.com/oauth/request_token","http://services.digg.com/oauth/access_token","http://digg.com/oauth/authorize");
System.out.println("Fetching request token from Digg...");
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 Digg...");
provider.retrieveAccessToken(pin);
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
URL url = new URL("http://services.digg.com/2.0/story.digg");
HttpURLConnection request = (HttpURLConnection)url.openConnection();
request.setDoOutput(true);
String charset="UTF-8";
request.setRequestProperty("story_id","20100706205455:22419803" );
request.setRequestProperty("key",consumer.getConsumerKey() );
request.setRequestProperty("secret",consumer.getConsumerSecret() );
request.setRequestProperty("access_token" ,consumer.getToken() );
request.setRequestProperty("access_token_secret",consumer.getTokenSecret() );
request.setRequestProperty("Accept-Charset", charset);
request.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
OutputStream output = null;
try {
output = request.getOutputStream();
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
}
System.out.print(request.getResponseCode());
例外情况如下:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://services.digg.com/2.0/story.digg
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1296)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1290)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:944)
at oauthapplication.Main.main(Main.java:97)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: http://services.digg.com/2.0/story.digg
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1241)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:373)
at oauthapplication.Main.main(Main.java:96)
400Java Result: 1