How to use Google Drive API from Google App Engine

2019-03-13 20:32发布

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

Searching usually results in pointers to DrEdit, including App Engine and Google Drive API, which asks the same question but accepts an answer I don't agree with, as DrEdit is an example app for Google Drive, not GAE.

The files list from the Drive API is what I'd like to be able to use from GAE: https://developers.google.com/drive/v2/reference/files/list

1条回答
做自己的国王
2楼-- · 2019-03-13 21:04

Although Google App Engine and Google Drive are both Google products, unfortunately they are not directly linked. Google Drive APIs can be accessed by the google-api-python-client library, which you have to install.

The process can be found at Python Google Drive API Quickstart Guide, and the summarized form is as follows:

  1. On Google's side: Allow Drive API Access for your GAE program

    • Activate Drive API. Click the Go to credentials button to continue...
    • Create your consent screen: Setup your OAuth Consent Screen as Google will throw weird errors if this has not been set up:
      • Click on the OAuth Consent Screen tab
      • Select an Email address and enter a Product name.
    • Get credentials:
      • Click on the Credentials tab
      • Select Add credentials and then OAuth 2.0 client ID. Choose your application type, and enter the relevant details. You can change them later!
      • Back on the Credentials tab, download the JSON credentials (all the way to the right in the table, the download button only appears when you hover near it). Rename it client_secret.json and place it in your root code directory. You will need this to request credentials from users.
  2. 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 the library which holds many Google product's APIs.

  3. Now you are ready to use the Drive API. You can test your access using the sample code. Read it because it's a good guide for writing your own code! If you are accessing user data, you will need to request user credentials when they log in and most probably store them. Then, to use the API, the easiest way would be to get the 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's 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. e.g. list first 10 files:
    results = service.files().list(maxResults=10).execute()
    # ... etc ... Do something with results
    

Above code snippet is modified from sample code.

The Reference API for Google Drive can be found here.

The same general procedure is required to link GAE to other Google product's APIs as well e.g. Calendar. All the best writing your program!

查看更多
登录 后发表回答