I've been looking for examples of authentication using the google oauth java package: https://code.google.com/p/google-oauth-java-client/
I've managed to find examples for oauth2 authentication using this package, but I can't find any for oauth1. The documentation gives a brief outline of a "typical application flow," but it leaves out all of the details.
Does anyone have any suggestions for where I could find examples of oauth1 authentication using thing package?
Based on google-oauth-java-client JavaDoc from Google's OAuth 1.0 guide and RFC 5849 the example should look as follows:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With