如何从Google App Engine使用Google Drive API? [英] How to use Google Drive API from Google App Engine?

查看:96
本文介绍了如何从Google App Engine使用Google Drive API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GAE(Python 2.7)中有一个应用程序,现在需要访问Google Drive才能显示(共享)文件夹和文档列表。

搜索通常会产生指向DrEdit的指针,包括,它提出相同的问题,但接受我不同意的答案,因为DrEdit是Google Drive的示例应用程序,而不是GAE。



Drive API中的文件列表是我希望能够从GAE使用的: https://developers.google.com/drive/v2/reference/files/list

解决方案

尽管Google App Engine和Google云端硬盘都是Google产品,但不幸的是它们并没有直接关联。 Google Drive API可以通过 google-api-python-client 库访问,您必须安装它。



您可以在 Python Google Drive API快速入门指南中找到该过程,并汇总表格如下:


  1. 在Google方面:允许您的GAE程序使用Drive API访问权限




  2. 方:下载 google-api-python-client library ,将其解压到您的代码目录并运行 python setup.py install 。这将安装包含许多Google产品API的库。

  3. 现在您已准备好使用Drive API。您可以使用示例代码测试您的访问权限。阅读它是因为它是编写自己的代码的好指南!如果您正在访问用户数据,则需要在登录时请求用户凭据,并且最有可能存储它们。然后,要使用API​​,最简单的方法是获取服务对象:

     从apiclient导入发现导入httplib2 

    $ b凭证= get_credentials()#您的请求/访问存储凭证的函数
    #使用用户凭证对Drive进行授权访问
    http = credentials.authorise(httplib2.Http())
    #服务对象是API函数的入口
    service = discovery.build('drive','v2',http = http)

    #使用服务对象运行您的请求。例如列出前10个文件:
    results = service.files()。list(maxResults = 10).execute()
    #... etc ...做一些结果


以上代码片段由示例代码

Google Drive的参考API可以请在此处找到



将GAE与其他Google产品的API连接也需要相同的一般程序日历。所有最好的写你的程序!


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

解决方案

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!

这篇关于如何从Google App Engine使用Google Drive API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆