通过 Google App Engine 的 Google Drive API [英] Google Drive API through Google App Engine

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

问题描述

我正在尝试通过 App Identity<使用 Google Drive API/a> 界面随 Google App Engine 提供.这基本上允许我的网络应用程序与 Google 的 API 从服务器到服务器进行通信.

I'm trying to use the Google Drive API through the App Identity interface provided with Google App Engine. This basically allows my web application to communicate with Google's APIs from server to server.

我不需要我的用户登录,我只需要显示我自己的 Google Drive 文档.

I don't need my users to login, I simply need to display my own Google Drive documents.

但是,在我设置了所有适当的值和范围,并在控制台页面上启用了所有正确的 Google Drive 旋钮后,我仍然通过对 https://www.googleapis.com 的简单 GET 请求获得此信息/drive/v2/files:

However, after I set all the appropriate values and scopes, and enable all the right Google Drive knobs on the console page, I still get this for a simple GET request to https://www.googleapis.com/drive/v2/files:

{ "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." }}

怎么了?我错过了什么?这是实际执行请求的代码 - 有趣的是,如果我使用其他 API(例如 URL 缩短器 API),它会很好用:

What's wrong? What am I missing? Here's the code that actually does the request - funny thing is that it works great if I use other APIs such as the URL shortener API:

var scopes = new java.util.ArrayList();                                                                                                    
scopes.add("https://www.googleapis.com/auth/drive");                                                                                       
var appIdentity = AppIdentityServiceFactory.getAppIdentityService();                                                                       
var accessToken = appIdentity.getAccessToken(scopes);

var url = new URL("https://www.googleapis.com/drive/v2/files");                                                                            
var connection = url.openConnection();                                                                                                     
connection.setDoOutput(true);                                                                                                              
connection.setRequestMethod("GET");                                                                                                        
connection.addRequestProperty("Content-Type", "application/json");                                                                         
connection.addRequestProperty("Authorization", "OAuth " + accessToken.getAccessToken());

编辑

例如,如果我只是将 API 更改为使用 urlshortner API,它会起作用:

If I simply change the API to use the urlshortner API for example, it works:

var url = new URL("https://www.googleapis.com/urlshortener/v1/url/history");

和输出:

{ "kind": "urlshortener#urlHistory", "totalItems": 0, "itemsPerPage": 30}

所以一定有什么东西不能与 Google Drive 和 App Identity 配合使用?

So there must be something not working with Google Drive and App Identity?

编辑 2

我从这里的正确答案中找到了一些帮助:https://stackoverflow.com/a/12526286/50394

I've found some help from the correct answer here: https://stackoverflow.com/a/12526286/50394

但它讨论的是在 Google Apps 上设置客户端 API 范围,我没有使用 Google Apps,我只是使用 Google App Engine 的域 foo.appspot.com

But it's talking about setting Client API scopes on Google Apps, and I'm not using Google Apps, I'm simply using Google App Engine's domain foo.appspot.com

推荐答案

您收到的 403 错误意味着您的 GET 中没有 Authorization 标头.逻辑是,如果没有 Authorization 标头,您就是匿名的(您是军团 blah blah :-)).匿名使用的驱动器配额为零,因此消息.URL 缩短器具有更高的匿名配额,因此它可以工作.

The 403 error you are getting means that there was no Authorization header in your GET. The logic is that without an Authorization header, you are anonymous (you are legion blah blah :-)). The Drive quota for anonymous use is zero, hence the message. URL shortener has a higher quota for anonymous so it works.

我建议您更改 URL 以指向您自己的 http 服务器,并检查您实际发送的标头.

I suggest you change the URL to point to an http server of your own, and check what headers you are actually sending.

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

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