从Appengine访问Google云端硬盘电子表格

时间:2014-04-04 12:40:32

标签: google-app-engine google-drive-api google-oauth google-spreadsheet-api

我有一个appengine应用程序需要访问Google云端硬盘上的单个硬编码电子表格。

到目前为止,我已经实现了以下目标:

SpreadsheetService service = new SpreadsheetService("myapp");
service.setUserCredentials("myusername@gmail.com", "myhardcodedpassword");

当我今天尝试使用新用户时,即使用户名和密码肯定正确,我也获得了InvalidCredentialsException。我的收件箱中收到了一封电子邮件,说我已经阻止了登录,并且似乎没有办法再次启用它们。

我也知道在源代码中硬编码密码是不好的做法。

但是,我已经在网上非常广泛地阅读了如何为此启用OAuth / OAuth2,并最终浪费了数小时和数小时拼接来自博客,stackoverflow答案等的信息片段,但无济于事。

理想情况下,解决方案将涉及生成长期访问令牌的初始过程,然后可以将其硬编码到应用程序中。

我想要一个明确的步骤列表,了解如何实现这一目标?

2 个答案:

答案 0 :(得分:11)

编辑:由于Google重新设计了API控制台,以下步骤的详细信息已更改 - 请参阅评论

好的,这就是一步一步

  1. 转到Google Cloud Console并注册您的项目(也称为应用程序)
  2. 您需要记下客户端ID和客户端密钥
  3. 转到OAuth Playground,点击齿轮图标,然后选择并输入您自己的凭据
  4. 系统会提醒您,您需要返回Cloud COnsole并将Oauth Playground添加为有效的回调网址。那样做。
  5. 执行步骤1,选择电子表格范围并单击“授权”
  6. 如果出现提示,请选择您的Google帐户,并在提示时授予身份验证
  7. 执行第2步,点击“兑换令牌代码”
  8. 您会看到一个包含刷新令牌
  9. 的输入框

    刷新令牌相当于您长期存在的用户名/密码,因此这就是您需要硬编码的地方(或存储您的应用可以检索它的安全地点。)

    当您需要访问Google Spreadsheets时,您将致电

    POST https://accounts.google.com/o/oauth2/token
    content-type: application/x-www-form-urlencoded
    client_secret=************&grant_type=refresh_token&refresh_token=1%2xxxxxxxxxx&client_id=999999999999.apps.googleusercontent.com
    

    将返回一个访问令牌

    {
      "access_token": "ya29.yyyyyyyyyyyyyyyyyy", 
      "token_type": "Bearer", 
      "expires_in": 3600
    }
    

    无论何时访问电子表格API

    ,都将访问令牌放入http标头中
    Authorization: Bearer ya29.yyyyyyyyyyyyyyyyy
    

    你已经完成了

答案 1 :(得分:0)

Pinoyyid确实提供了很棒的帮助。我想跟进Python代码,以便访问个人(可通过网络访问)的Google云端硬盘。

这在Google App Engine中可以很好地运行(作为网络应用程序的一部分),也可以在桌面上独立运行(假设您的计算机上安装了Google App Engine SDK [可从以下网址获取:https://developers.google.com/appengine/downloads])。

就我而言,我在Pinyyid的过程中添加了https://www.googleapis.com/auth/drive范围,因为我希望能够访问我的所有Google云端硬盘文件。

按照Pinoyyid的说明获取刷新令牌等后,这个小Python脚本将获得Google驱动器上所有文件的列表:

import httplib2
import datetime
from oauth2client.client import OAuth2Credentials
from apiclient.discovery import build

API_KEY = 'AIz...'       # from "Key for Server Applications" from "Public API Access" section of Google Developers Console
access_token = "ya29..." # from Piinoyyid's instructions
refresh_token = "1/V..." # from Piinoyyid's instructions
client_id = '654....apps.googleusercontent.com' # from "Client ID for web application" from "OAuth" section of Google Developers Console
client_secret = '6Cl...' # from "Client ID for web application" from "OAuth" section of Google Developers Console
token_expiry = datetime.datetime.utcnow() - datetime.timedelta(days=1)
token_uri = 'https://accounts.google.com/o/oauth2/token'
user_agent = 'python urllib (I reckon)'

def main():
    service = createDrive()
    dirlist = service.files().list(maxResults=30)
    print 'dirlist', dirlist
    result = dirlist.execute()
    print 'result', result

def createDrive():
    credentials = OAuth2Credentials(access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent)
    http = httplib2.Http()
    http = credentials.authorize(http)
    return build('drive', 'v2', http=http, developerKey=API_KEY)

main()

我很感谢所有提供解决方案的人员。

相关问题