GAE TaskQueue:从 App Engine 外部访问拉取队列的示例代码?

发布于 2024-11-28 08:37:44 字数 548 浏览 5 评论 0原文

我正在尝试使用 GAE TaskQueue 的 REST API 将任务从队列拉取到外部服务器(不在 GAE 上的服务器)。

  • 是否有一个库可以为我执行此操作?

  • API 很简单,所以我只需要弄清楚身份验证。我使用 --dump_request 检查了 gtaskqueue_samplegoogle-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 from google-api-python-client using --dump_request and found the authorization: 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 to https://accounts.google.com/o/oauth2/token fails with No 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不喜欢何必死缠烂打 2024-12-05 08:37:44

这个问题很旧,但仍然适用,所以我将根据我最近的经验尝试更好的答案。

可以在 appengine 之外访问拉取任务队列,但正如提问者所说,没有好的例子,所以这里有一个更深入的指南。就我而言,我有一个自定义 python 脚本,需要轮询队列以查找要运行的新作业。

在采用此方法之前,您还可以选择滚动自己的安全性并为 appengine 任务队列调用创建一个简单的 Web 包装器。在处理完这个问题后,我很想走这条路,但由于这是有效的,所以我现在正在使用它。

设置您的计算机

设置您的帐户

  • 使用 Google Cloud Console ,创建一个注册的应用程序(如果您还没有。单击您的 AppEngine 项目 -> API 和身份验证 -> 注册的应用程序。您可以输入名称和应用程序类型,然后接受默认值。创建后,记下客户端 ID和客户端密钥供稍后使用。

  • 。屏幕(API 和身份验证 ->同意屏幕)。请注意,您首次设置您的 oauth 凭据时只需要此同意屏幕。您需要输入电子邮件地址和产品名称(我还输入了主页网址)。

生成 OAuth 凭据

  • 您只需生成一次凭据文件,然后它将用于您的 python 脚本中的未来调用。运行此 python 代码,该代码将打开浏览器并生成凭证文件。此代码的参考位于此处

    from oauth2client.tools import run
    从 oauth2client.client 导入 OAuth2WebServerFlow
    从 oauth2client.file 导入存储
    导入gflags
    标志 = gflags.FLAGS
    
    存储 = 存储('credentials.json')
    
    流 = OAuth2WebServerFlow(client_id='',
                           client_secret='',
                           范围='https://www.googleapis.com/auth/taskqueue',
                           redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    
    凭证 = 运行(流、存储)
    

进行任务队列调用

  • 确保您已在 AppEngine 中添加拉取队列queue.yaml,以及您在上面的 oauth 步骤中使用的电子邮件地址。

    from oauth2client.tools import run
    从 oauth2client.client 导入 OAuth2WebServerFlow
    从 oauth2client.file 导入存储
    从 apiclient.discovery 导入构建
    导入httplib2
    
    存储 = 存储('credentials.json')
    凭证 = storage.get()
    http = httplib2.Http()
    http = 凭证.授权(http)
    task_api = build('taskqueue', 'v1beta2')
    tasks = task_api.tasks().lease(project='<您的 appengine 项目>',taskqueue='<拉取队列名称>', numTasks=1,leaseSecs=600).execute(http=http)
    任务 = 任务['项目'][0]
    有效负载 = 任务['payloadBase64']
    有效负载 = base64.b64decode(有效负载)
    
    #然后做你的工作并在完成后删除任务
    
    task_api.tasks().delete(project='s~<您的 appengine 项目>',taskqueue='<拉取队列名称>', task=task['id']).execute(http=http)
    
  • 任务队列API 参考< /p>

  • 请注意删除调用中项目名称前面的前缀“s~”。仅当我添加此内容并且我相信它是 错误

更新 7/1/2014

因此,实际上有一种更简单的方法来进行服务器到服务器的调用。这种方式不需要您使用“流程”(登录到谷歌)来获取访问密钥。

设置您的计算机

设置您的帐户

  • 使用 Google Cloud Console ,创建一个注册应用程序(如果您没有已经有一个。单击您的 AppEngine 项目 -> API 的 & 身份验证 -> 单击“创建新客户端 ID”,然后单击“创建客户端 ID”。
    会弹出一个下载框来下载您的私钥,将其保存到您的代码目录(或任何地方,我保存为 client_key.p12)。在 Web 界面上,记下客户端 ID 和电子邮件。

替换上面的凭据代码

from oauth2client.client import SignedJwtAssertionCredentials

email = '<***>.gserviceaccount.com'
f = file('client_key.p12', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(email,
                                            key,
                                            scope='https://www.googleapis.com/auth/taskqueue')

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.

    from oauth2client.tools import run
    from oauth2client.client import OAuth2WebServerFlow
    from oauth2client.file import Storage
    import gflags
    FLAGS = gflags.FLAGS
    
    storage = Storage('credentials.json')
    
    flow = OAuth2WebServerFlow(client_id='<your_client_id>',
                           client_secret='<your_client_secret>',
                           scope='https://www.googleapis.com/auth/taskqueue',
                           redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    
    credentials = run(flow, storage )
    

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.

    from oauth2client.tools import run
    from oauth2client.client import OAuth2WebServerFlow
    from oauth2client.file import Storage
    from apiclient.discovery import build
    import httplib2
    
    storage = Storage('credentials.json')
    credentials = storage.get()
    http = httplib2.Http()
    http = credentials.authorize(http)
    task_api = build('taskqueue', 'v1beta2')
    tasks = task_api.tasks().lease(project='<your appengine project>',taskqueue='<pull queue name>', numTasks=1, leaseSecs=600).execute(http=http)
    task = tasks['items'][0]
    payload = task['payloadBase64']
    payload = base64.b64decode(payload)
    
    #then do your work and delete the task when done
    
    task_api.tasks().delete(project='s~<your appengine project>',taskqueue='<pull queue name>', task=task['id']).execute(http=http)
    
  • Task Queue API Reference

  • Note the prefix 's~' in front of the project name in the delete call. It would only work if I added this and I believe it is a bug.

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

  • Using Google Cloud Console, create a Registered App (if you don't already have one. Click on your AppEngine project -> API's & Auth -> Credentials. Click Create New Client ID, specify Service Account, then click Create Client ID.
    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

from oauth2client.client import SignedJwtAssertionCredentials

email = '<***>.gserviceaccount.com'
f = file('client_key.p12', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(email,
                                            key,
                                            scope='https://www.googleapis.com/auth/taskqueue')
你的背包 2024-12-05 08:37:44

这些 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.

自在安然 2024-12-05 08:37:44

拉取队列页面的整个部分介绍了客户端库和示例代码。

The pull queues page has a whole section about client libraries and sample code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文