Examples for oauth1 using google-api-java-oauth

I searched for authentication examples using google oauth java package: https://code.google.com/p/google-oauth-java-client/

I managed to find examples for oauth2 authentication using this package, but I can not find them for oauth1. The documentation provides a brief overview of the "typical application flow", but it does not contain all the details.

Does anyone have any suggestions on where I can find oauth1 authentication examples using the stuff package?

+9
source share
2 answers

Based on the google-oauth-java-client JavaDoc from the Google OAuth 1.0 manual and RFC 5849, the example should look like this:

OAuthHmacSigner signer = new OAuthHmacSigner(); // Get Temporary Token OAuthGetTemporaryToken getTemporaryToken = new OAuthGetTemporaryToken(TOKEN_SERVER_URL); signer.clientSharedSecret = OAuth2ClientCredentials.CONSUMER_SECRET; getTemporaryToken.signer = signer; getTemporaryToken.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY; getTemporaryToken.transport = new NetHttpTransport(); OAuthCredentialsResponse temporaryTokenResponse = getTemporaryToken.execute(); // Build Authenticate URL OAuthAuthorizeTemporaryTokenUrl accessTempToken = new OAuthAuthorizeTemporaryTokenUrl(AUTHENTICATE_URL); accessTempToken.temporaryToken = temporaryTokenResponse.token; String authUrl = accessTempToken.build(); // Redirect to Authenticate URL in order to get Verifier Code System.out.println(authUrl); // Get Access Token using Temporary token and Verifier Code OAuthGetAccessToken getAccessToken = new OAuthGetAccessToken(ACCESS_TOKEN_URL); getAccessToken.signer = signer; getAccessToken.temporaryToken=temporaryTokenResponse.token; getAccessToken.transport = new NetHttpTransport(); getAccessToken.verifier= "VERIFIER_CODE"; getAccessToken.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY; OAuthCredentialsResponse accessTokenResponse = getAccessToken.execute(); // Build OAuthParameters in order to use them while accessing the resource OAuthParameters oauthParameters = new OAuthParameters(); signer.tokenSharedSecret = accessTokenResponse.tokenSecret; oauthParameters.signer = signer; oauthParameters.consumerKey = OAuth2ClientCredentials.CONSUMER_KEY; oauthParameters.token = accessTokenResponse.token; oauthParameters.verifier = "VERIFIER_CODE"; // Use OAuthParameters to access the desired Resource URL HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(oauthParameters); GenericUrl genericUrl = new GenericUrl("RESOURCE_URL"); HttpResponse response = requestFactory.buildGetRequest(genericUrl).execute(); System.out.println(response.parseAsString()); 

Hope this helps.

+10
source

The above example was extremely useful.

A note on using this library with non-standard implementations of oAuth 1.0. I used the Goodreads oAuth API , and this seems to be what the oAuth Bible calls the โ€œunsuccessful version of OAuth 1.0a 3-Legged,โ€ which means that it does not send the verifier code back after redirecting the authorized user back to your return URL. In this case, you need to delete all lines related to VERIFIER_CODE above and add:

 signer.tokenSharedSecret = temporaryTokenResponse.tokenSecret; 

before the line:

 OAuthCredentialsResponse accessTokenResponse = getAccessToken.execute(); 

Spent me sorting it out, so hopefully someone else helps.

+2
source

All Articles