Q:

Examples for oauth1 using google-api-java-oauth

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?

A:

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.

A:

The above example was extremely useful.
A note on using this lib with non-standard oAuth 1.0 implementations. I was using the Goodreads oAuth API, and it appears to be what the oAuth Bible calls a "failed OAuth 1.0a 3-Legged implementation", meaning that it doesn't send back a verifier code after redirecting the authorized user back to your callback URL. In this case, you will need to remove any lines referring to VERIFIER_CODE above and add:
signer.tokenSharedSecret = temporaryTokenResponse.tokenSecret;

before the line:
OAuthCredentialsResponse accessTokenResponse = getAccessToken.execute();

Took me a while to figure out, so hopefully helps someone else.

