Simple way to use the google contacts API from a python script
<p>I'm currently trying to write a script and I would need to access my GMail contacts to finish it. I tried googling that a bit and it looks like there are a few libraries to do that, but some answers seems very old, and some others aren't that clear.</p>

<p>What is the correct way currently to access the contacts API from a script ? Using <a href="https://github.com/google/gdata-python-client" rel="nofollow">https://github.com/google/gdata-python-client</a> I assume ?</p>

<p>Any recent response seems to involve the user copying a link into his browser by hand, getting redirected to some non-existent URL and being asked to copy that URL back in the script to parse the code in it. That seems perfectly ridiculous, I do hope there is a proper way to access the API for non-web applications ?</p>

<p>I'll be the only user of that script so I don't mind having to put my full e-mail address and password in if that makes it easier, as long as I don't have to keep re-authenticating by hand. Just want to authorize it once and have it work forever. I'm just trying to find the contact's name from the phone number so I don't even need to access everything.</p>

<p>Here is what I'm using in the end :</p>

<pre><code>if arg == "--init":
    gflow = OAuth2WebServerFlow(client_id='&lt;ID&gt;', client_secret='&lt;secret&gt;',
                            scope='https://www.googleapis.com/auth/contacts.readonly',
                            redirect_uri='http://localhost');
    gflow.params['access_type'] = 'offline';
    auth_uri = gflow.step1_get_authorize_url();
    weechat.prnt("", "Please go to " + auth_uri + " and come back here with the code. Run /gauth code");
else:
    credentials = gflow.step2_exchange(arg);
    storage.put(credentials);
    http = httplib2.Http();
    http = credentials.authorize(http);
    people = build('people', 'v1', http=http);
</code></pre>

<p>Works fine with that. I do need to copy / paste the URL by hand and then get the code out of the redirection, but since I put the access_type to offline I have to do that only once.</p>

python google-api google-api-python-client