I'm following https://dev.twitter.com/web/sign-in/implementing to implement OAuth signup in my application with twitter.
Here is the service which presents the User the authorize this app dialog from twitter:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("/twitter")
public Response redirectToTwitter() {
Configuration cfg = new ConfigurationBuilder().setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.build();
Twitter twitter = new TwitterFactory(cfg).getInstance();
String callbackURL = "https://localhost:9090/app/ui/oauth/twitter";
try {
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
String authURL = requestToken.getAuthenticationURL();
return Response.seeOther(URI.create(authURL)).build();
} catch (TwitterException e) {
LOG.error(e.getMessage(), e);
return ErrorResponse.create(e.getMessage());
}
}
This works and redirects the browser to the Twitter Page which asks for authorization. When I click Sign In the redirect dialog appears which redirect me to a URL something like:
@GET
@Path("/twitter")
public SocialSignInView login(@QueryParam("oauth_token") String token,
@QueryParam("oauth_verifier") String verifier) {
Configuration cfg = new ConfigurationBuilder().setOAuthConsumerKey(TWITTER_CONSUMER_KEY)
.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET)
.setOAuthAccessToken(token)
.build();
Twitter twitter = new TwitterFactory(cfg).getInstance();
String callbackURL = "https://localhost:9090/app/ui/oauth/twitter";
String screenName = null;
try {
RequestToken requestToken = twitter.getOAuthRequestToken(callbackURL);
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
screenName = accessToken.getScreenName();
} catch (TwitterException e) {
LOG.error(e.getMessage(), e);
}
return new SocialSignInView(screenName);
}
At this point I have all the required parameters - according to https://dev.twitter.com/web/sign-in/implementing 3. - to retrieve an access token, however, I don't know how to put together a RequestToken object form the existing oauth_token
.
With the code above I'm receiving the following error:
ERROR 13:33:06.478 [dw-165 - GET /app/ui/oauth/twitter?oauth_token=6oWQxQAAAAAAgyORAAABTtmVVFM&oauth_verifier=GMX5SiqnkFfUu2MgirTDJnkJmtHZXn5H] r.d.d.resources.SocialSignInCompleteResource: 401:Authentication credentials (https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have set valid consumer key/secret, access token/secret, and the system clock is in sync.
Error processing your OAuth request: Invalid oauth_verifier parameter