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

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

问题描述

我试图通过应用程序标识与Google App Engine一起提供的界面。这基本上允许我的Web应用程序与Google的API 从服务器到服务器进行通信。



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



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

  {error:{errors:[ {domain:usageLimits,reason:dailyLimitExceededUnreg,message:未经验证的使用的每日限制超出,继续使用需要注册。,extendedHelp:https://code.google.com / apis / console}],code:403,message:未经验证的使用的每日限制超出,继续使用需要注册。 }} 

有什么不对?我错过了什么?以下是实际执行请求的代码 - 有趣的是,如果我使用其他API(例如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 =新网址(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(授权,OAuth+ accessToken.getAccessToken());

编辑
<例如,如果我简单地将API更改为使用 urlshortner API,它就可以工作:

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



和输出:

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



所以肯定有一些东西不适用于Google云端硬盘和应用程序标识?

编辑2



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



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

解决方案

你得到的403错误意味着GET中没有授权标头。逻辑是,没有授权标题,你是匿名的(你是军团等等等等:-))。用于匿名使用的驱动器配额为零,因此为消息。 URL shortener拥有较高的匿名配额,因此它可以工作。



我建议您将URL更改为指向您自己的http服务器,然后检查您实际上的标头发送。

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.

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

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." }}

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());

EDIT

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");

And output:

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

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

EDIT 2

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

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

解决方案

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.

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天全站免登陆