Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Examples for oauth1 using google-api-java-oauth [closed]

Tags:

java

oauth

api

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?

like image 973
user2130237 Avatar asked Mar 04 '13 03:03

user2130237


1 Answers

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.

like image 187
Sqeezer Avatar answered Oct 18 '22 10:10

Sqeezer