如何登录到appengine中的任意用户以使用Drive SDK? [英] How can I log in to an arbitrary user in appengine for use with the Drive SDK?

查看:101
本文介绍了如何登录到appengine中的任意用户以使用Drive SDK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序需要登录到一个单独的Drive帐户,并使用cron作业自动对这些文件执行操作。最初,我尝试使用域管理员登录来执行此操作,但是我无法对域管理员进行任何测试,因为您似乎无法将域名管理员帐户用于测试服务器,这使得测试我的应用程序变得不可能!

因此,我开始考虑存储仲裁oauth令牌 - 尤其是刷新令牌 - 在初始设置后自动登录到此帐户。但是,所有的API和文档都假设多个用户都是手动登录的,而且我无法在oauth API中找到允许或记录除当前登录用户之外的任何内容的功能。



我怎样才能在测试域上测试我的代码?我可以在没有编写自己的oauth库并手动执行oauth请求的情况下执行此操作吗?或者是否有办法让域管理员授权在本地测试服务器上工作?

解决方案

您可以将凭据使用可启用的远程API 将单个帐户存入您的数据存储区在你的 app.yaml 文件中:

  builtins:
- remote_api:on

执行

  remote_api_shell.py -s your_app_id.appspot.com 

命令行,您将有权访问可在应用程序环境中执行的shell。在执行此操作之前,请确保已部署应用程序(更多信息请参见下面的本地开发),并确保<$ c包含 google-api-python-client 的源代码$ c> pip - 安装并运行 enable-app-engine-project / path / to / project 将其添加到您的App Engine项目中。



一旦进入远程shell(执行上述远程命令后),执行以下操作:

<$从oauth2client.appengine导入CredentialsModel
从oauth2client.appengine导入StorageByKeyName
从oauth2client.client导入OAuth2WebServerFlow
导入运行

KEY_NAME ='your_choice_here'
CREDENTIALS_PROPERTY_NAME ='凭证'
SCOPE ='https://www.googleapis.com/auth/drive'

storage = StorageByKeyName (CredentialsModel,KEY_NAME,CREDENTIALS_PROPERTY_NAME)
flow = OAuth2WebServerFlow(
client_id = YOUR_CLIENT_ID,
client_se cret = YOUR_CLIENT_SECRET,
scope = SCOPE)

run(flow,storage)

注意:如果您没有使用 google-api-python-client 代码部署您的应用程序,则这会失败,因为您的应用程序赢得了'不知道如何在本地机器上制作相同的导入,例如 from oauth2client.appengine import CredentialsModel


$ b $ < run 被调用,您的Web浏览器将打开,并提示您接受您使用 CLIENT_ID CLIENT_SECRET指定的客户端的OAuth访问并成功完成后,它将在部署的应用程序的数据存储中保存 CredentialsModel 的实例 your_app_id.appspot.com ,它会使用您提供的 KEY_NAME 来存储它。



通过执行

  storage = StorageByKeyName(CredentialsModel,KEY_NAME),应用程序中的任何调用者(包括您的cron作业)都可以访问这些凭据,CREDENTIALS_PROPERTY_NAME)
credentials = storage.get()

本地开发:



如果您想在本地进行测试,您可以通过
dev_appserve在本地运行应用程序r.py --port = PORT / path / to / project

您可以使用远程API shell执行相同的命令并将它指向本地应用程序:

  remote_api_shell.py -s localhost:PORT 

在这里,您可以执行您在远程api shell中执行的相同代码,并且类似地, CredentialsModel 的实例将存储在如上所述,如果您没有正确的 google-api-python-client

编辑:这用于建议在以下位置使用交互式控制台:

  http:// localhost:PORT / _ah / admin / interactive 

,但发现这不起作用,因为 socket 在App Engine本地开发沙箱中无法正常工作。


I have an application that needs to log into a singular Drive account and perform operations on the files automatically using a cron job. Initially, I tried to use the domain administrator login to do this, however I am unable to do any testing with the domain administrator as it seems that you cannot use the test server with a domain administrator account, which makes testing my application a bit impossible!

As such, I started looking at storing arbitray oauth tokens--especially the refresh token--to log into this account automatically after the initial setup. However, all of the APIs and documentation assume that multiple individual users are logging in manually, and I cannot find functionality in the oauth APIs that allow or account for logging into anything but the currently logged in user.

How can I achieve this in a way that I can test my code on a test domain? Can I do it without writing my own oauth library and doing the oauth requests by hand? Or is there a way to get the domain administrator authorization to work on a local test server?

解决方案

You can load the credentials for a single account into your datastore using the Remote API, which can be enabled in your app.yaml file:

builtins:
- remote_api: on

By executing

remote_api_shell.py -s your_app_id.appspot.com

from the command line you'll have access to a shell which can execute in the environment of your application. Before doing this, make sure you have your application deployed (more on local development below) and make sure the source for google-api-python-client is included by pip-installing it and running enable-app-engine-project /path/to/project to add it to your App Engine project.

Once you are in the remote shell (after executing the remote command above), perform the following:

from oauth2client.appengine import CredentialsModel
from oauth2client.appengine import StorageByKeyName
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

KEY_NAME = 'your_choice_here'
CREDENTIALS_PROPERTY_NAME = 'credentials'
SCOPE = 'https://www.googleapis.com/auth/drive'

storage = StorageByKeyName(CredentialsModel, KEY_NAME, CREDENTIALS_PROPERTY_NAME)
flow = OAuth2WebServerFlow(
    client_id=YOUR_CLIENT_ID,
    client_secret=YOUR_CLIENT_SECRET,
    scope=SCOPE)

run(flow, storage)

NOTE: If you have not deployed your application with the google-api-python-client code, this will fail, because your application won't know how to make the same imports you made on your local machine, e.g. from oauth2client.appengine import CredentialsModel.

When run is called, your web browser will open and prompt you to accept the OAuth access for the client you've specified with CLIENT_ID and CLIENT_SECRET and after successfully completing, it will save an instance of CredentialsModel in the datastore of the deployed application your_app_id.appspot.com and it will store it using the KEY_NAME your provided.

After doing this, any caller in your application -- including your cron jobs -- can access those credentials by executing

storage = StorageByKeyName(CredentialsModel, KEY_NAME, CREDENTIALS_PROPERTY_NAME)
credentials = storage.get()

Local Development:

If you'd like to test this locally, you can run your application locally via dev_appserver.py --port=PORT /path/to/project

and you can execute the same commands using the remote API shell and pointing it at your local application:

remote_api_shell.py -s localhost:PORT

Once here, you can execute the same code you did in the remote api shell and similarly an instance of CredentialsModel will be stored in the datastore of your local development server.

As above, if you don't have the correct google-api-python-client modules included, this will fail.

EDIT: This used to recommend using the Interactive Console at:

http://localhost:PORT/_ah/admin/interactive

but it was discovered that this doesn't work because socket does not work properly in the App Engine local development sandbox.

这篇关于如何登录到appengine中的任意用户以使用Drive SDK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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