GAE TaskQueue:从 App Engine 外部访问拉取队列的示例代码?
我正在尝试使用 GAE TaskQueue 的 REST API 将任务从队列拉取到外部服务器(不在 GAE 上的服务器)。
是否有一个库可以为我执行此操作?
API 很简单,所以我只需要弄清楚身份验证。我使用
--dump_request
检查了gtaskqueue_sample
从google-api-python-client
发送的请求,并找到了authorization: OAuth XXX
标头。将该令牌添加到我自己的请求中是有效的,但令牌似乎会定期(可能每天)过期,并且我不知道如何重新生成它。就此而言,gtaskqueue_sample 本身不再起作用(对https://accounts.google.com/o/oauth2/token
的调用失败,并显示无法解码 JSON 对象
)。
如何进行身份验证?这是一个服务器应用程序,因此理想情况下我可以生成一个可以从那时起使用的令牌。
I'm attempting to use GAE TaskQueue's REST API to pull tasks from a queue to an external server (a server not on GAE).
Is there a library that does this for me?
The API is simple enough, so I just need to figure out authentication. I examined the request sent by
gtaskqueue_sample
fromgoogle-api-python-client
using--dump_request
and found theauthorization: OAuth XXX
header. Adding that token to my own requested worked, but the token seems to expire periodically (possibly daily), and I can't figure out how to re-generate it. For that matter, gtaskqueue_sample itself no longer works (the call tohttps://accounts.google.com/o/oauth2/token
fails withNo JSON object could be decoded
).
How does one take care of authentication? This is a server app so ideally I could generate a token that I could use from then on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个问题很旧,但仍然适用,所以我将根据我最近的经验尝试更好的答案。
可以在 appengine 之外访问拉取任务队列,但正如提问者所说,没有好的例子,所以这里有一个更深入的指南。就我而言,我有一个自定义 python 脚本,需要轮询队列以查找要运行的新作业。
在采用此方法之前,您还可以选择滚动自己的安全性并为 appengine 任务队列调用创建一个简单的 Web 包装器。在处理完这个问题后,我很想走这条路,但由于这是有效的,所以我现在正在使用它。
设置您的计算机
设置您的帐户
使用 Google Cloud Console ,创建一个注册的应用程序(如果您还没有。单击您的 AppEngine 项目 -> API 和身份验证 -> 注册的应用程序。您可以输入名称和应用程序类型,然后接受默认值。创建后,记下客户端 ID和客户端密钥供稍后使用。
生成 OAuth 凭据
您只需生成一次凭据文件,然后它将用于您的 python 脚本中的未来调用。运行此 python 代码,该代码将打开浏览器并生成凭证文件。此代码的参考位于此处。
进行任务队列调用
确保您已在 AppEngine 中添加拉取队列queue.yaml,以及您在上面的 oauth 步骤中使用的电子邮件地址。
任务队列API 参考< /p>
更新 7/1/2014
因此,实际上有一种更简单的方法来进行服务器到服务器的调用。这种方式不需要您使用“流程”(登录到谷歌)来获取访问密钥。
设置您的计算机
设置您的帐户
会弹出一个下载框来下载您的私钥,将其保存到您的代码目录(或任何地方,我保存为 client_key.p12)。在 Web 界面上,记下客户端 ID 和电子邮件。
替换上面的凭据代码
This question is old, but it still applies, so I am going to attempt a better answer based on my recent experience.
It is possible to access pull task queues outside of appengine but as the asker stated, there are no good examples, so here is a more in depth guide. In my case I had a custom python script that needed to poll the queue for new jobs to run.
Before taking this route, you also have the option of rolling your own security and making a simple web wrapper to the appengine taskqueue calls. I was tempted to go that route after dealing with this, but since this is working I'm using it for now.
Setup Your Machine
Setup Your Account
Using Google Cloud Console, create a Registered App (if you don't already have one. Click on your AppEngine project -> API's and auth -> Registered Apps. You can enter a name and application type and then accept the defaults. Once it is created, note the Client Id and Client Secret for later.
Also Update your Consent Screen (API's and auth -> Consent Screen). Note that you will only need this consent screen to setup your oauth credentials for the first time. You will need to enter Email Address and a Product Name (I entered a HomePage Url also).
Generate OAuth Credentials
You only need to generate a credentials file once, then it will be used for future calls in your python script. Run this python code which opens a browser and generates the credential file. A reference for this code is here.
Make Your Taskqueue Calls
Make sure you have added a pull queue in your AppEngine queue.yaml, with the email address you used in the oauth step above.
Task Queue API Reference
Update 7/1/2014
So there is actually an easier way to make server to server calls. This way doesn't require you using the "flow" (logging on to google) to get an access key.
Setup Your Machine
Setup Your Account
A download box will pop up to download your private key, save this to your code directory (or wherever, I saved as client_key.p12). On the web interface, note the Client ID and Email.
Replace the Credential Code from Above
这些 API 仅适用于 GAE 服务器,因为只能通过queue.yaml 创建队列,并且事实上 API 不会公开任何用于插入队列和任务或项目的 API。
These APIS work only for GAE server since the queues can be created only via queue.yaml and infact API does not expose any API for inserting queue and tasks or project.
拉取队列页面的整个部分介绍了客户端库和示例代码。
The pull queues page has a whole section about client libraries and sample code.