Search code examples
pythonauthenticationgoogle-drive-apigdata

GData: How to avoid hardcoding login info


I am working on a reporting system which automatically updates results overnight and puts them in files on a google drive. The way it is working right now is by hardcoding the login and password information which is by no means ideal even if it works. A search in StackOverflow does not point this question specifically, which surprises me. A very simplified example with the relevant sections of code looks like:

import gdata.docs.service

class GDrive(object):
    def __init__(self, email, password)
        self.gd_client = gdata.docs.service.DocService()
        self.gd_client.ClientLogin(email, password)
    def upload(self):
        # Code to Upload file somewhere in GDrive.

gd = GDrive("the@email.address", "ThePassword")
gd.upload()

Can something be done to avoid writing explicitly the username and password?


Solution

  • I would make use of the OAuth2 protocol. It is a secure way to store credentials for a long time (but not forever).

    A bit of a short answer from my cell, but check:

    https://developers.google.com/drive/about-auth

    And this bit makes working with Oauth2 a lot easier:

    https://developers.google.com/api-client-library/python/platforms/google_app_engine#Decorators