Reputation: 693
My task is to connect to Google Drive API (with the help of PyDrive module) and download some files. I somehow managed to get this thing working on my local computer - I registered my "app" at the Google Console, I downloaded the client_secret.json
, ran the script, the authentification window popped out, I signed in with my Google account and Drive was accessible, everything OK.
Now I want to use my script on a server and I am basically clueless how to do that. I submitted a support ticket to my provider and their answer was:
You need to get these:
{
"#authJson": "{"access_token":"XXX","token_type":"Bearer","expires_in":3600,"refresh_token":"YYY","created":1457455916}",
"#appKey": "key",
"#appSecret": "secret"
}
where #authJson
is a result of 'whoevers-drive-you-want-to-access' authorization and
and #appKey
a #appSecret
come from the oauth.
I don't know how exactly get these. I know how to download client_secret.json
. So the question is: how to get these? And am I even on the right track? Or different approach is required.
The ideal final state would be:
to have some sort of permanent access_token
to my Google Drive which I can pass to the app (e.g. as a string parameter). The app then connects to my Drive and downloads desired files.
Upvotes: 5
Views: 6322
Reputation: 2310
EDIT: See edit for pre-authorised server applications.
What you are looking for is CommandLineAuth()
from PyDrive.
Your code should look something like this:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
ga = GoogleAuth()
ga.CommandLineAuth() # This line in your code currently calls LocalWebserverAuth()
drive = GoogleDrive(self.ga)
# etc.
When you run the script on the server, it will ask you to copy-paste a link into your local browser. Once you login with a specific account, you will be given a seemingly random string of letters and numbers. Paste that string into your console and you should be good to go.
Since you likely won't want to do this everytime the script runs, consider adding a settings.yaml
file to your project, which allows you to save log-in credentials. Details of how this is setup can be found in the docs.
EDIT: If you want to distribute a PyDrive script to any server without further authentication, you have to:
client_secret_xxxxx.json
settings.yaml
file to your project, see this template. Ensure that save_credentials_file:
is set, e.g. creds.json
client-secret
from the client_secret_xxxx.json
into the settings.yaml
file.creds.json
)settings.yaml
file, and c) the generated creds.json
file to the remote machine. Make sure relative paths don't change.Note: There are API call-limits attached to the Google Drive API preventing more than 100 calls within a 100 second period.
Upvotes: 9
Reputation: 117281
You need to be authenticated to access someone's private drive account. They will have to do this via a web browser the first time once they have granted you access you should just save the the refresh token above. The refresh token will enable you to access their data when ever you need from your server sided script. See: Python Quickstart
If you will only be access a drive account that you personally control then check out service accounts Using OAuth 2.0 for Server to Server Applications
Upvotes: 0