I am trying to use Yahoo Mail Web Services API but I am getting an error when trying to authenticate using OAth.
I am using signpost API to authenticate.
This is the code I have:
CODEBOX
public class Main {
public static void main(String[] args) throws Exception {
OAuthConsumer consumer = new CommonsHttpOAuthConsumer("dj0yJmk9Uk5TVVBRZUdxN2VLJmQ9WVdrOVkyZGpZWEJPTm04bWNHbzlOVGsxT0RVeE16azAmc=1j
b25zdW1lcnNlY3JldCZ4PTdk",
"e9b7fadccda0ff23e0d7c76accbd509a7688f955", SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider(consumer,
"https://api.login.yahoo.com/oauth/v2/get_request_token",
"https://api.login.yahoo.com/oauth/v2/get_token",
"https://api.login.yahoo.com/oauth/v2/request_auth");
System.out.println("Fetching request token from Yahoo...");
// we do not support callbacks, thus pass OOB
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 verification code and hit ENTER when you're done");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
System.out.println("Fetching access token from Fire Eagle...");
provider.retrieveAccessToken(code);
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
HttpPost request = new HttpPost(
"https://fireeagle.yahooapis.com/api/0.1/update");
StringEntity body = new StringEntity("city=hamburg&label="
+ URLEncoder.encode("Send via Signpost!", "UTF-8"));
body.setContentType("application/x-www-form-urlencoded");
request.setEntity(body);
consumer.sign(request);
System.out.println("Sending update request to Fire Eagle...");
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
System.out.println("Response: "
+ response.getStatusLine().getStatusCode() + " "
+ response.getStatusLine().getReasonPhrase());
}
}
But I get an error when I try to execute that:
Fetching request token from Yahoo...
Exception in thread "main" oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
at oauth.signpost.basic.DefaultOAuthProvider.retrieveToken(DefaultOAuthProvider.jav
a:126)
at oauth.signpost.basic.DefaultOAuthProvider.retrieveRequestToken(DefaultOAuthProvi
der.java:66)
at Main.main(Main.java:46)
Java Result: 1
Anyone can help me getting this code to run?
Regards