Google Contacts API - Auth2.0

I am looking for a good way to get every email address of my contacts from the google account for the desktop application in Python.

The first time I created the application through Google Code. I switched the Google Plus API, extracting most of my user data, but not from my contacts.

I started an investigation and I found many things, but most of them are out of date.

I found a good way to get my contacts using the gdata library, but giving me full read / write access on it, https://www.google.com/m8/feeds without feedback.

self.gd_client = gdata.contacts.client.ContactsClient(source='MyAppliName') self.gd_client.ClientLogin(email, password, self.gd_client.source) 

According to the official "google contact api" google group that has been ported to stackoverflow, read only access is compromised.

By the way, I'm not a big fan of "I trust my application, I use read-only access, I swear."

I found the google api playground in https://developers.google.com/oauthplayground in which they use the OAuth2.0 token with most of the apis, including the contact, by switching the Webpage:

The Google OAuth 2.0 Playground is requesting permission to:

  • Contact management

According to this playground, you can use OAuth2.0 with google contact api, but I have no idea how to add https://www.google.com/m8/feeds to my scope, which does not appear on the list.

Is there any other way to do this?

+4
source share
2 answers

If this question is still open for you, here is a sample code on how to use oauth2 and the Google v3 Contacts API:

 import gdata.contacts.client from gdata.gauth import AuthSubToken from oauth2client import tools from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage def oauth2_authorize_application(client_secret_file, scope, credential_cache_file='credentials_cache.json'): """ authorize an application to the requested scope by asking the user in a browser. :param client_secret_file: json file containing the client secret for an offline application :param scope: scope(s) to authorize the application for :param credential_cache_file: if provided or not None, the credenials will be cached in a file. The user does not need to be reauthenticated :return OAuth2Credentials object """ FLOW = flow_from_clientsecrets(client_secret_file, scope=scope) storage = Storage(credential_cache_file) credentials = storage.get() if credentials is None or credentials.invalid: # Run oauth2 flow with default arguments. credentials = tools.run_flow(FLOW, storage, tools.argparser.parse_args([])) return credentials SCOPES = ['https://www.google.com/m8/feeds/', 'https://www.googleapis.com/auth/userinfo.email'] credentials = oauth2_authorize_application('client-secret.json', scope=SCOPES) token_string = credentials.get_access_token().access_token # deprecated! # auth_token = AuthSubToken(token_string, SCOPES) with open('client-secret.json') as f: oauth2_client_secret = json.load(f) auth_token = gdata.gauth.OAuth2Token( client_id=oauth2_client_secret['web']['client_id'], client_secret=oauth2_client_secret['web']['client_secret'], scope=SCOPES, user_agent='MyUserAgent/1.0', access_token=credentials.get_access_token().access_token, refresh_token=credentials.refresh_token) client = gdata.contacts.client.ContactsClient(auth_token=auth_token) query = gdata.contacts.client.ContactsQuery() 
+4
source

The request should look like this:

 https://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds& state=<myState>& redirect_uri=<Redirect URI>& response_type=code& client_id=<my Client ID>&approval_prompt=force 

This will allow read / write access to the user's contacts.

+1
source

All Articles