从Appengine访问Google Drive电子表格 [英] Accessing a Google Drive spreadsheet from Appengine

查看:117
本文介绍了从Appengine访问Google Drive电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



到目前为止,我一直在实现这一点,如下所示:

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

当我今天用新用户试用时,得到 InvalidCredentialsException code>即使用户名和密码绝对正确。我在收件箱中收到一封电子邮件,表示怀疑登录已被阻止,似乎无法再次启用它们。



我也知道硬编码密码然而,我已经在网上阅读了非常广泛的关于如何为此启用OAuth / OAuth2,并且最终浪费了几小时和几小时来拼凑碎片的问题。信息来自博客,stackoverflow的答案等,无济于事。



理想情况下,解决方案将涉及一个初始过程来生成一个长期存取令牌,编码到应用程序中。



我需要一个确定的步骤列表来说明如何实现此目的?

解决方案

编辑:由于Google重新设计了API控制台,以下步骤的细节已更改 - / strong>



好的,一步一步


  1. 转到Google云控制台并注册您的项目(aka应用程序)

  2. 您需要注意客户端ID和客户端密钥

  3. 转到OAuth Playground,单击齿轮图标并选择并输入您自己的凭据

  4. 您将被提醒您需要返回Cloud COnsole并将Oauth Playground添加为有效的回调网址。

  5. 执行步骤1,选择电子表格范围并点击授权

  6. 如果出现提示,请选择您的Google帐户, li>
  7. 执行第2步,点击交换授权代码。

  8. 您将看到一个包含刷新令牌的输入框

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



当您需要访问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 .yyyy yyyyyyyyyyyyyy,
token_type:持票人,
expires_in:3600
}

无论您何时访问电子表格API,都要将访问令牌放入http标头中

  Authorization:Bearer ya29.yyyyyyyyyyyyyyyyy 

您完成了

I have an appengine app that needs to access a single, hard-coded spreadsheet on Google Drive.

Up until now I have been achieving this as follows:

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

When I tried this today with a new user, I got InvalidCredentialsException even though the username and password were definitely correct. I got an email in my inbox saying suspicions sign-ins had been prevented, and there seems to be no way to enable them again.

I am also aware that hardcoding passwords in source is bad practice.

However, I have read very widely online for how to enable OAuth/OAuth2 for this, and have ended up wasting hours and hours piecing fragments of information from blogs, stackoverflow answers etc, to no avail.

Ideally the solution would involve an initial process to generate a long-lived access token, which could then be hard-coded in to the app.

I want a definitive list of steps for how to achieve this?

解决方案

EDIT: As Google have redesigned the API Console, the details of the steps below have changed - see comments

OK here goes, step by step

  1. Go to Google Cloud Console and register your project (aka application)
  2. You need to note the Client ID, and Client Secret
  3. Go to the OAuth Playground, click the gear icon and choose and enter your own credentials
  4. You will be reminded that you need to go back to the Cloud COnsole and add the Oauth Playground as a valid callback url. So do that.
  5. Do Step 1, choosing the spreadsheet scope and click authorize
  6. Choose your Google account if prompted and grant auth when prompted
  7. Do Step 2, Click 'Exchange auth code for tokens'
  8. You will see an input box containing a refresh token

The refresh token is the equivalent of your long lived username/password, so this is what you'll hard code (or store someplace secure your app can retrieve it).

When you need to access Google Spreadsheets, you will call

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

which will return you an access token

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

Put the access token into an http header for whenever you access the spreadsheet API

Authorization: Bearer ya29.yyyyyyyyyyyyyyyyy

And you're done

这篇关于从Appengine访问Google Drive电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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