How to use Google Drive API from Google App Engine?

I have an application in GAE (Python 2.7), and now you need access to Google Drive to display a (general) list of folders and documents.

A search usually leads to pointers to DrEdit, including the App Engine and the Google Drive API , which asks the same question, but accepts an answer that I disagree with, since DrEdit is an example application for Google Drive, not GAE.

The list of files from the drive APIs is what I would like to use from GAE: https://developers.google.com/drive/v2/reference/files/list

+6
source share
1 answer

Although the Google App Engine and Google Drive are Google products, unfortunately they are not directly related. Google Drive APIs can be accessed through the google-api-python-client library that you must install.

This process can be found in the Python Drive API Quick Start Guide for Google Drive , and the summarized form is as follows:

  • On the Google side: Allow API access for the API for your GAE program

    • Activate Drive API . Click Go to Credentials to continue ...
    • Create a consent screen: Set your OAuth consent screen as Google will throw strange errors if this has not been configured:
      • Click the OAuth Consent Screen tab
      • Select Email Address and enter the Product Name .
    • Get credentials:
      • Go to the Credentials tab
      • Select Add Credentials , and then OAuth 2.0 Client ID . Select your application type and enter the appropriate data. You can change them later!
      • Go back to the Credentials tab, load the JSON credentials (everything is right in the table, the download button will appear only when you approach it). Rename it client_secret.json and put it in the root directory. You will need this to request user credentials.
  • On your side: Download the google-api-python-client library , unzip it in your code directory and run python setup.py install . This will install a library containing many Google product APIs.

  • You are now ready to use the Drive API. You can check your access using the sample code . Read this because it is a good guide for writing your own code! If you are accessing user data, you will need to request the user credentials when they log in and most likely saved them. Then, to use the API, the easiest way would be to get a service object:

     import httplib2 from apiclient import discovery credentials = get_credentials() #Your function to request / access stored credentials #Authorise access to Drive using the user credentials http = credentials.authorise(httplib2.Http()) #The service object is the gateway to your API functions service = discovery.build('drive', 'v2', http=http) #Run your requests using the service object. eg list first 10 files: results = service.files().list(maxResults=10).execute() # ... etc ... Do something with results 

Above the code snippet, the sample code has been changed.

The reference API for Google Drive can be found here .

The same general procedure is required to associate GAE with other Google product APIs, for example, for example. The calendar. All the best that is written in your program!

+4
source

Source: https://habr.com/ru/post/944884/


All Articles