Two Legged OAuth and Creating contacts

24 views
Skip to first unread message

Brett

unread,
Nov 6, 2009, 7:09:39 PM11/6/09
to Google Contacts API
I've been able to successfully use the Java TwoLeggedOAuthExample:

http://code.google.com/p/gdata-java-client/source/browse/trunk/java/sample/oauth/TwoLeggedOAuthExample.java

In the process of ripping out the code I don't need for my purposes,
I've taken out the interactivity and used ContactsService instead of
the generic GoogleService. I can pull the Contacts feed just fine,
but I cannot create new contacts:

// works fine
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters
();
oauthParameters.setOAuthConsumerKey(consumerKey);
oauthParameters.setOAuthConsumerSecret(consumerSecret);
oauthParameters.setScope("http://www.google.com/m8/feeds/
contacts/");

// Finally create a new GoogleOAuthHelperObject. This is the
object you
// will use for all OAuth-related interaction.
OAuthSigner signer = new OAuthHmacSha1Signer();
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

ContactsService cService = new ContactsService("Bulk-Contacts");
cService.setOAuthCredentials(oauthParameters, signer);

URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/
default/base?xoauth_requestor_id=" + email);
System.out.println("Sending request to " + feedUrl.toString());

// Make the request to Google
ContactFeed resultFeed = cService.getFeed(feedUrl,
ContactFeed.class);
System.out.println
("=====================================================");
System.out.println("TITLE: " + resultFeed.getTitle().getPlainText
());

// Does not work (same cService used, already successfully
authenticated and feed pulled)

URL postUrl = new URL("http://www.google.com/m8/feeds/contacts/" +
email + "/full");
ContactEntry ce = getContact();
System.out.print(cService.insert(postUrl, ce));

I get: "Caused by:
com.google.gdata.client.authn.oauth.OAuthException: oauth_token does
not exist."

Do I need additional steps to POST to create a contact that I don't
need when just pulling a feed?

Thanks,

-b

Julian (Google)

unread,
Nov 17, 2009, 5:59:47 AM11/17/09
to Google Contacts API
Hi

I think there is a problem in the order you set the OAuth Credentials,
please try the following:

ContactsService service = new ContactsService("contacts-tests-
script");

OAuthSigner signer = new OAuthHmacSha1Signer();
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(domain);
oauthParameters.setOAuthConsumerSecret(consumerSecret);
oauthParameters.setScope("http://www.google.com/m8/feeds/");
service.setOAuthCredentials(oauthParameters, signer);
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

full/?xoauth_requestor_id=user%40domain.com");

ContactEntry entry = new ContactEntry();
Name name = new Name();
FullName fullName = new FullName("TestContact","TestContact");
name.setFullName(fullName);
entry.setName(name);
Email email = new Email();
email.setAddress("testContact"+"@domain.com");
email.setLabel("home");
entry.addEmailAddress(email);
service.insert(feedUrl,entry);

Cheers,
Julian

On Nov 7, 12:09 am, Brett <bret...@gmail.com> wrote:
> I've been able to successfully use the Java TwoLeggedOAuthExample:
>
> http://code.google.com/p/gdata-java-client/source/browse/trunk/java/s...
Reply all
Reply to author
Forward
0 new messages