Migrate from GDATA to Google API Java Client

45 views
Skip to first unread message

Alexandre Jacquet

unread,
Jun 14, 2011, 11:11:08 AM6/14/11
to Google Apps APIS
Hello Guys,

I'm trying to migrate most of my code to Google API Java Client however i'm stuck right in the beginning.

Can anyone give me any light how should i make the requests for the OAuth3legged.

I have already seen all the WIkis on the API sites however i can't find any clues.



Tks!



                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882




Michael Manoochehri

unread,
Jun 14, 2011, 11:19:10 AM6/14/11
to google-app...@googlegroups.com
Hi Alexandre:

A really great example of using OAuth with Gdata is here:

It walks you through each of the steps of the OAuth dance, and the code is fairly compact.

- Michael

Michael Manoochehri

unread,
Jun 14, 2011, 11:21:21 AM6/14/11
to google-app...@googlegroups.com
Also, I recommend using the HMAC-SHA1 method for this sample, as you can simply provide your consumer key and secret without providing a private key to sign OAuth requests.

Michael.

Alexandre Jacquet

unread,
Jun 14, 2011, 11:28:31 AM6/14/11
to google-app...@googlegroups.com
What about the inverse using OAuth in Google API JAVA Client.


I can't find any resource about this.



                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882






--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-apps-mgmt-apis/-/Tr1sHnp7PwgJ.
To post to this group, send email to google-app...@googlegroups.com.
To unsubscribe from this group, send email to google-apps-mgmt...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

Alexandre Jacquet

unread,
Jun 14, 2011, 3:43:16 PM6/14/11
to google-app...@googlegroups.com
Hello Guys,

I'm basing on the BUZZ OAUTH Sample that is available at http://code.google.com/p/google-api-java-client/

However in stuck in this part, anyone can help me?

GoogleOAuthGetTemporaryToken temporaryToken = new GoogleOAuthGetTemporaryToken();
//temporaryToken.transport = Util.AUTH_TRANSPORT;
signer = new OAuthHmacSigner();
signer.clientSharedSecret = ConsumerCredentials.OAUTH_CONSUMER_KEY;
temporaryToken.signer = signer;
temporaryToken.consumerKey = ConsumerCredentials.OAUTH_CONSUMER_SECRET;
temporaryToken.scope = "https://apps-apis.google.com/a/feeds/";
temporaryToken.displayName = AppInfo.APP_DESCRIPTION;
System.out.println(temporaryToken.build());
OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
signer.tokenSharedSecret = tempCredentials.tokenSecret;


It returns a NullPointer however i can't find what is missing


Exception in thread "main" java.lang.NullPointerException
at com.google.api.client.auth.oauth.AbstractOAuthGetToken.execute(AbstractOAuthGetToken.java:69)
at GroupSettingTests.main(GroupSettingTests.java:30)


                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882






On Tue, Jun 14, 2011 at 12:21 PM, Michael Manoochehri <manoo...@google.com> wrote:
Also, I recommend using the HMAC-SHA1 method for this sample, as you can simply provide your consumer key and secret without providing a private key to sign OAuth requests.

Michael.

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Aaron

unread,
Jun 14, 2011, 7:47:38 PM6/14/11
to google-app...@googlegroups.com
Alexandre-

All of the google-api-client classes are open source, I would recommend that you download them as it helps a bunch when trying to troubleshoot this type of thing. In this case, if you look at line 69 of the AbstractOAuthGetToken class you can see that the only possibility for a null pointer exception is if you have a null transport. From your code above I see that you have commented out that line, so I'm going to assume that's why you're getting your exception.

If you want to be able to execute the temporary token request you're going to have to set a transport on the temporary token. The transport you use will vary depending on the environment you're in, but it can be as easy as just new NetHttpTransport().

Also, you may find it more helpful for posting questions pertaining to the Google API Java Client in its specific group: https://groups.google.com/group/google-api-java-client?pli=1

-Aaron

Alexandre Jacquet

unread,
Jun 14, 2011, 7:49:39 PM6/14/11
to google-app...@googlegroups.com
Thank you Aaron,

I didn't know about the other group.

About Null Point even with that line uncommented it raises the exception.

=\


                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882






--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Aaron

unread,
Jun 14, 2011, 7:55:59 PM6/14/11
to google-app...@googlegroups.com
Alexandre-

I'm not sure what you have behind "Util.AUTH_TRANSPORT" but I'm guessing that that's where your problem lies. My guess is that somehow that is still null. If you just set it equal to "new NetHttpTransport()" do you still get the null pointer exception?

-Aaron

Alexandre Jacquet

unread,
Jun 14, 2011, 8:02:45 PM6/14/11
to google-app...@googlegroups.com
Hey,

After the change you suggested I'm raising now a 400 Bad Request, i think the problem is i'm missing an attribute =\ i'll dig a little deeper however it would be great if google could create a raw sample :P



Here is the output raised by the exception.

Exception in thread "main" com.google.api.client.http.HttpResponseException: 400 Bad Request
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:380)
at com.google.api.client.auth.oauth.AbstractOAuthGetToken.execute(AbstractOAuthGetToken.java:73)
at GroupSettingTests.main(GroupSettingTests.java:31)


                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882








-Aaron

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Aaron

unread,
Jun 14, 2011, 8:14:02 PM6/14/11
to google-app...@googlegroups.com
Alexandre-

You also need to set the callback parameter. You can find that information about the parameters that you need here: http://code.google.com/apis/accounts/docs/OAuth_ref.html

The following may help you... The following illustrates the process for setting up OAuth 1.0 using the google-api-java-client for a client application (non-web application):

// Step 1: Get OAuth Request Token

GoogleOAuthGetTemporaryToken temporaryToken =
    new GoogleOAuthGetTemporaryToken();
temporaryToken.callback = "oob";
signer.clientSharedSecret = <CLIENT_SHARED_SECRET_HERE>;
temporaryToken.signer = signer;
temporaryToken.consumerKey = <CONSUMER_KEY_HERE>;
temporaryToken.scope = "https://apps-apis.google.com/a/feeds";
temporaryToken.transport = new NetHttpTransport();
OAuthCredentialsResponse tempCreds = temporaryToken.execute();

// Step 2: Get OAuth Authorize Token
signer.tokenSharedSecret = tempCreds.tokenSecret;
GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl = new GoogleOAuthAuthorizeTemporaryTokenUrl();
authorizeUrl.temporaryToken = tempCreds.token;
String authorizationUrl = authorizeUrl.build();
System.out.println("Go to this authorizationUrl: " + authorizationUrl);
BufferedReader inStream = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Paste verification code: ");
String verifier = inStream.readLine();

// Step 3: Get OAuth Access Token
GoogleOAuthGetAccessToken accessToken = new GoogleOAuthGetAccessToken();
accessToken.temporaryToken = tempCreds.token;
accessToken.signer = signer;
accessToken.consumerKey = <CONSUMER_KEY_HERE>;
accessToken.verifier = verifier;
accessToken.transport = new NetHttpTransport();
OAuthCredentialsResponse creds = accessToken.execute();
signer.tokenSharedSecret = creds.tokenSecret;

-Aaron

Alexandre Jacquet

unread,
Jun 14, 2011, 8:35:59 PM6/14/11
to google-app...@googlegroups.com
Ok two things,

I should use 

signer.tokenSharedSecret for Domains? and signer.clientSharedSecret for Gmail applications or is non-sense?

For this sample that you have sent is returning the same error.


GoogleOAuthGetTemporaryToken temporaryToken = new GoogleOAuthGetTemporaryToken();
temporaryToken.callback = "oob";
signer = new OAuthHmacSigner();
signer.tokenSharedSecret = consumerSecret;
temporaryToken.signer = signer;
temporaryToken.consumerKey = consumerKey;
temporaryToken.scope = "https://apps-apis.google.com/a/feeds";
temporaryToken.transport = new NetHttpTransport();
OAuthCredentialsResponse tempCreds = temporaryToken.execute();

Exception in thread "main" com.google.api.client.http.HttpResponseException: 400 Bad Request
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:380)
at com.google.api.client.auth.oauth.AbstractOAuthGetToken.execute(AbstractOAuthGetToken.java:73)
at GroupSettingTests.main(GroupSettingTests.java:33)


url GoogleOAuthGetTemporaryToken  (id=18)
callback "oob" (id=27)
classInfo ClassInfo  (id=32)
consumerKey "ajsolutions.me" (id=34)
displayName "appTest" (id=31)
fragment null
host "www.google.com" (id=35)
keySet null
pathParts ArrayList<E>  (id=36)
port -1
scheme "https" (id=42)
signer OAuthHmacSigner  (id=46)
clientSharedSecret ----consumerSecret----- (id=124)
tokenSharedSecret ----consumerSecret----- (id=124)
transport NetHttpTransport  (id=49)
unknownFields ArrayMap<K,V>  (id=52)
usePost false
values null


                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882






-Aaron

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Aaron

unread,
Jun 14, 2011, 9:04:00 PM6/14/11
to google-app...@googlegroups.com
Hmm...  Don't know what to say. The example works fine for me, the only thing I can guess is that somehow your key and secret aren't right, but if they were working in GData I would assume that they would work in the api-java-client.

From the documentation it says:
HTTP 400 Bad Request: in the case of an unsupported or missing parameter, an unsupported oauth_signature_method, or other error in the request format or content.
Which isn't super helpful. You could try turning on logging for the api-java-client so you know what your request looks like and make sure that it matches up with the documentation.
 

Alexandre Jacquet

unread,
Jun 14, 2011, 9:07:15 PM6/14/11
to google-app...@googlegroups.com
Well, I will post the question alse in Google API Java Client since is more a Client issue.

Thank you for all the help aaron!


                                                                                           

Alexandre Jacquet

Google Apps Deployment Specialst 
São Paulo | SP | Brasil
Tel.: 55 11 8064 6882






--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.
Reply all
Reply to author
Forward
0 new messages