Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving all (Other) Contacts from Google Contacts API using gdata-python-client and OAuth2 token?

In the hunt to list all (to include Other) Contacts for a Gmail/GSuite user. The current People API does not support this functionality, noting the following threads:

  • Found this thread here, confirming such change in the API: Google Contacts API vs People API
  • Google team noting it here: https://groups.google.com/forum/#!topic/google-contacts-api/iLsrN23xF6g
  • Referencing ticket request for prioritization here: https://issuetracker.google.com/issues/36757468

When diving deeper, it seems the Contacts API is still functioning and can be used via gdata https://developers.google.com/contacts/v3/

However, based on the following repo (https://github.com/google/gdata-python-client), there's limited documentation on implementation using OAuth2 (userID, token, refreshToken), which is the current stumbling block to get the list of Other Contacts

Any help would be greatly appreciated, thanks!

like image 422
Andrew Stroup Avatar asked Jun 14 '26 12:06

Andrew Stroup


1 Answers

I found this posting https://gist.github.com/jorilallo/3686737 from 7 years ago(?). The actual sample code below that I had to modify a bit to get it working:

import gdata
import gdata.gauth
import gdata.contacts.client
import json
import requests

GOOGLE_CLIENT_ID = 'GOOGLE_CLIENT_ID'  # Provided in the APIs console
GOOGLE_CLIENT_SECRET = 'GOOGLE_CLIENT_SECRET'  # Provided in the APIs console
ACCESS_TOKEN = 'ACCESS_TOKEN' # given from a prior OAuth2 workflow, along with userID and refreshToken
REFRESH_TOKEN = 'REFRESH_TOKEN'

# GData with access token
token = gdata.gauth.OAuth2Token(
    client_id=GOOGLE_CLIENT_ID,
    client_secret=GOOGLE_CLIENT_SECRET,
    scope='https://www.google.com/m8/feeds',
    user_agent='app.testing',
    access_token=ACCESS_TOKEN,
    refresh_token=REFRESH_TOKEN)

contact_client = gdata.contacts.client.ContactsClient()
token.authorize(contact_client)

feed = contact_client.GetContacts()

for entry in feed.entry:
  entry.title.text
  for e in entry.email:
    e.address

# JSON with access token
r = requests.get('https://www.google.com/m8/feeds/contacts/default/full?access_token=%s&alt=json&max-results=50&start-index=0' % (access_token))
data = json.loads(r.text)
print data
like image 97
Andrew Stroup Avatar answered Jun 16 '26 04:06

Andrew Stroup



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!