Automating pydrive verification process
First, you're misunderstanding one very important bit of how this works:
when I try to use the above script while I am logged into another
account. It doesn't upload the eng.txt into my gdrive that generated
the secret_client.json but the account that was logged in when I
authorize the authentication
This is exactly how it's supposed to work. You, as the developer, distribute client_secret.json
with your application, and that file is used by PyDrive to authenticate the application with Google. Google wants to know how many API requests are being made by each application out there for all sorts of reasons (metrics, charge the account, revoke access, etc.), so it requires the application to authenticate itself.
Now, when your application runs LocalWebserverAuth
, it's authenticating the client with Google. The client, of course, is the person actually using your application. In this case, the developer and client are the same person (you), but imagine your want to distribute your application to a million different people. They need to be able to authenticate themselves and upload files to their own Drive account, rather that having them all end up in yours (the developer), who provided client_secret.json
.
That said, it's really just a very minor change to make it so your app doesn't have to ask the client to authenticate every time you run the app. You just need to use LoadCredentialsFile
and SaveCredentialsFile
.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
textfile = drive.CreateFile()
textfile.SetContentFile('eng.txt')
textfile.Upload()
print textfile
drive.CreateFile({'id':textfile['id']}).GetContentFile('eng-dl.txt')
Service Account and PyDrive
After some more research it seems like I needed to install pycrypto
to fix the import error described above. I don't know why as it is not specified in the error message.
Fail to authenticate with pydrive to google drive
I made changes according to this Solution by Dano. The save and load of mycreds.txt
helped to solve this issue.
Related Topics
Subprocess.Popen() Error (No Such File or Directory) When Calling Command with Arguments as a String
Time.Sleep -- Sleeps Thread or Process
Multiprocessing in Python - Sharing Large Object (E.G. Pandas Dataframe) Between Multiple Processes
Attaching a Decorator to All Functions Within a Class
File Not Found Error When Launching a Subprocess Containing Piped Commands
How to Automatically Fix an Invalid JSON String
How to Install Python Package from Github
How to Use Groupby to Concatenate Strings in Python Pandas
Why Sum on Lists Is (Sometimes) Faster Than Itertools.Chain
Catching an Exception While Using a Python 'With' Statement
Runtimeerror: Main Thread Is Not in Main Loop
Programmatically Searching Google in Python Using Custom Search
How Does a Python for Loop with Iterable Work
Make a Post Request While Redirecting in Flask