I'm trying to access user data from a site that supports OAuth 1.0 without callbacks. I'm using the signpost library and I'm not able to retrieve the access token because the site I'm trying to connect to doesn't require the user to specify a verification code. To get the access token from signpost, it seems I need to provide some type of verification code.
MY QUESTION: Is there a way to bypass this step and still obtain an access token?
Otherwise, I get the message "Authorization failed (server replied with a 401)". I've set up/registered my client.
I've included my existing code and more detail about the site I'm trying to connect to (http://www.stepgreen.org - you'd have to create an account to see the information below):
Authentication: OAuth
StepGreens preferred method of authentication is OAuth. For more information on OAuth you should visit OAuth.net. We currently support OAuth 1.0 without callbacks.
You will need some additional information to configure your OAuth library开发者_如何转开发 of choice:
request a Request Token: /oauth/request_token request an Access Token: /oauth/access_token Authorize URL: /oauth/authorize To use OAuth with your clients, remember to setup your Clients.
public static void main(String[] args) throws Exception{
OAuthConsumer consumer = new DefaultOAuthConsumer( CONSUMER_KEY, CONSUMER_SECRET,
SignatureMethod.HMAC_SHA1);
OAuthProvider provider = new DefaultOAuthProvider(consumer, REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTHORIZE_URL);
System.out.println("Fetching request token...");
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 verificationCode = br.readLine();
System.out.println("Fetching access token...");
String verificationCode = consumer.getToken();
provider.retrieveAccessToken(verificationCode.trim());
System.out.println("Access token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
URL url = new URL("http://www.stepgreen.org/api/v1/users/username/ted5000s.xml");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
InputStream stream = (InputStream)request.getContent();
System.out.println("Result = " + convertStreamToString(stream));
System.out.println("Sending request...");
request.connect();
System.out.println("Response code: " + request.getResponseCode() + " " +
request.getResponseMessage());
}
Response:
Fetching request token...
Request token: hidden for privacy reasons
Token secret: hidden for privacy reasons
Now visit: http://www.stepgreen.org/oauth/authorize?oauth_token=hidden ... and grant this app authorization Enter the verification code and hit ENTER when you're done:
Fetching access token... 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.java:126) at oauth.signpost.basic.DefaultOAuthProvider.retrieveAccessToken(DefaultOAuthProvider.java:96) at Test.main(Test.java:89)
精彩评论