15
votes

I'd like to save the files of my Saas Appication to my Google Drive Account, all the examples I've seen was with oauth2 autentication and needs the end user autenticate openning the browser, I need to upload files from my server without any user interation, sending files direct to my account!

I have try many tutorials I found on internet with no success, mainly the oficial

Google Drive API with Python

How can I autenticate programatically from my server and upload files and use API features such as share folders and others?

I'm using Python, the lib PyDrive uses the same aproach to autenticate

5

5 Answers

14
votes

You can do this, but need to use a Service Account, which is (or rather can be used as) an account for an application, and doesn't require a browser to open.

The documentation is here: https://developers.google.com/api-client-library/python/auth/service-accounts

And an example (without PyDrive, which is just a wrapper around all this, but makes service account a bit trickier):

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http

scopes = ['https://www.googleapis.com/auth/drive.readonly']

credentials = ServiceAccountCredentials.from_json_keyfile_name('YourDownloadedFile-5ahjaosi2df2d.json', scopes)

http_auth = credentials.authorize(Http())
drive = build('drive', 'v3', http=http_auth)

request = drive.files().list().execute()
files = request.get('items', [])
for f in files:
    print(f)
1
votes

I know it's quite late for answer but this worked for me:

Use the same API you were using, this time in your computer, it will generate a Storage.json which using it along with your scripts will solve the issue (specially in read-ony platforms like heroku)

0
votes

Checkout the Using OAuth 2.0 for Web Server Applications. It seems that's what you're looking for.

Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.

Open the Credentials page in the API Console. Click Create credentials OAuth client ID. Complete the form. Set the application type to Web application. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized redirect URIs. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080.

We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page.

0
votes

To add to andyhasit's answer, using a service account is the correct and easiest way to do this.

The problem with using the JSON key file is it becomes hard to deploy code anywhere else, because you don't want the file in version control. An easier solution is to use an environment variable like so:

https://benjames.io/2020/09/13/authorise-your-python-google-drive-api-the-easy-way/

0
votes

Might be a bit late but I've been working with gdrive over python, js and .net and here's one proposed solution (REST API) once you get the authorization code on authorization code How to refresh token in .net google api v3?

Please let me know if you have any questions