Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way authenticate with Python Google APIs to access own accounts

The main use case is using IPython as CLI to my own Google accounts. What I am really after is minmizing the fussing around between starting the IPython shell and actually issuing usefull calls against the API.

The docs for authenticating with Google APIs focus on setting up application which other user will use to access their data. This leads to a lengthy Oauth dance involving a browser in order to allow other users to authenticate without compromising their credential. However, I do not mind sharing my private credentials with myself. I am not planning on sharing the code. If I did share the code I would use something like dotenv to separate the credentials from the code.

Twitter provides developers a second set of credentials that allows developers to access their own accounts for testing. Thus it is possible to access ones own account programmatically by just providing to sets of credentials: the developer credentials that allow the calls to the API and the other credentials that grant access to the developers own data. For example:

from twitter import *

t = Twitter(
    auth=OAuth(token, token_key, con_secret, con_secret_key))

# Get your "home" timeline
t.statuses.home_timeline()

# Update your status
t.statuses.update(
    status="Tweeting from Python")

Where con_secret* are the developer credentials and and token* are the account access credentials.

  • How can I do something equally simple with Google APIs?
  • Where can I get credentials to access my own account?
  • How would I use them in Google API?

As an example what would be the simplest procedure for retrieving the contents from one of my own Youtube playlists?


I have com to think that a Python headless browser library could be give me what I need. I have asked a related question on SE Software Recommendations https://softwarerecs.stackexchange.com/questions/35744/python-headless-browser-library-for-oauth2-authentication-from-ipython-console

like image 573
Daniel Mahler Avatar asked Aug 19 '16 21:08

Daniel Mahler


1 Answers

I would like to download a set of credentials

Google offers this ability through it's client_secrets.json file. There are different ways to download this, depending on the type of account you want to use (Web application, installed application, Service account). The different techniques can be found here .

Store the credentials locally and keep using them without requiring new credentials every call

This also isn't a problem, the client secret is valid until you renew it - AFAIK there is no automatic expiry unless you specify otherwise.

Once you have downloaded your client_secrets.json, store the file in a non-public directory (normally inside your project directory/config).

Similar to the downloading of the file, there are different techniques (flow classes) to use the JSON file depending on what type of account you are using. As an example, the below would be used for installed and web applications;

from oauth2client.client import OAuth2WebServerFlow
...
flow = OAuth2WebServerFlow(client_id='your_client_id',
                           client_secret='your_client_secret',
                           scope='scope URL here',
                           redirect_uri='http://example.com/auth_return')

Other flow class examples can be found here

Hope this helps - If you need further information, the official documentation (which be warned, can be incredibly inaccurate and confusing) can be found here https://developers.google.com/api-client-library/python/guide/aaa_oauth

like image 160
JayIsTooCommon Avatar answered Nov 11 '22 13:11

JayIsTooCommon