On last week I found very interesting library on java for integration with oauth services, meet – . It also works well on Android.
So, let’s start – I needed to implement some functionality for interacting with twitter, and here I met an issue. I decided to use callback instead of pin and for this case twitter sends another format of data. So, let’s have a look:
import org.scribe.model.Token; import org.scribe.oauth.OAuthService; import org.scribe.builder.ServiceBuilder; import org.scribe.builder.api.TwitterApi; OAuthService service = new ServiceBuilder() .provider(TwitterApi.class) .apiKey(API_KEY) .apiSecret(API_SECRET) .callback(CALLBACK_URL) .build();
And after that let’s take auth url for twitter:
String authUrl = service.getAuthorizationUrl(service.getRequestToken());
When you let application connect to your account twitter will redirect you to specified callback with two parameters oauth_token and oauth_verifier. What we need to do for success (I show the code that uses resteasy library):
@GET
@Path("/twitter_callback")
public String getTwitterVerifier(@QueryParam(value = "oauth_token") String oauth_token,
@QueryParam(value = "oauth_verifier") String oauth_verifier) {
Token reqToken = mService.getRequestToken();
Token reqToken2 = new Token(oauth_token, reqToken.getSecret());
Token accessToken = mService.getAccessToken(reqToken2, new Verifier(oauth_verifier));
return accessToken.toString();
}
And voila! We have valid access token. The main idea was in creating new request token with received oauth token.
It’s not so obvious and I have not found it in the documentation