[英]Google App Engine Task Queue REST API for the Java Development Server (devserver)
I have a GAE app running on the Java Development Server (AKA devserver) on my local machine that defines some Task Queue pull queues. 我在本地计算机上的Java开发服务器(AKA devserver)上运行了一个GAE应用程序,该应用程序定义了一些“任务队列”提取队列。
I want to use the Task Queue REST API from a different standalone Java app, running on my machine, to access these pull queues. 我想使用计算机上运行的其他独立Java应用程序中的Task Queue REST API来访问这些拉入队列。
I tried the sample suggested by Google's documentation that implements leasing tasks. 我尝试了 Google 文档中建议的示例 , 该示例实现了租赁任务。 Apparently, it uses the Google API Client Library for Java that requires a client_secrets.json
file of the format: 显然,它使用Java的Google API客户端库,该库要求格式为client_secrets.json
文件:
{
"installed": {
"client_id": "retrieved from creating a Service Account for the project",
"client_secret": "supposedly appears in the JSON file that's downloaded upon creation of a Service Account for the project"
}
}
I created a Service Account for the project and a JSON was automatically downloaded, with the following format: 我为该项目创建了一个服务帐户,并使用以下格式自动下载了JSON:
{
"type": "service_account",
"project_id": "myProjectId",
"private_key_id": "some long hexadecimal",
"private_key": "an even longer encrypted stuff",
"client_email": "myServiceAccountName@myProjectId.iam.gserviceaccount.com",
"client_id": "a decimal number",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/myServiceAccountName%40myProjectId.iam.gserviceaccount.com"
}
In my client_secrets.json
file I used private_key_id
as the client_secret
value and executed the sample code. 在我的client_secrets.json
文件中,我使用private_key_id
作为client_secret
值并执行了示例代码。
The result was that it opened https://accounts.google.com/o/oauth2/auth
in my browser, which displayed an error with status code 400
and the following message: 结果是它在我的浏览器中打开了https://accounts.google.com/o/oauth2/auth
,显示状态码为400
的错误和以下消息:
Error: redirect_uri_mismatch Application: My Application The redirect URI in the request: http://localhost:61130/Callback did not match a registered redirect URI. 错误:redirect_uri_mismatch应用程序:我的应用程序请求中的重定向URI: http:// localhost:61130 / Callback与注册的重定向URI不匹配。
I tried Taskqueue.Builder.setRootUrl("http://localhost:8080")
, although its javadoc says not to mess with it. 我尝试了Taskqueue.Builder.setRootUrl("http://localhost:8080")
,尽管它的javadoc说不要弄乱它。 It returned 401 Unauthorized
. 它返回401 Unauthorized
。
client_secret
1 in the client_secrets.json
file when I do want to lease tasks from Google App Engine on the cloud? 当我想从云上的Google App Engine租用任务时,应使用什么作为client_secrets.json
文件中client_secret
1的值? 1 When I created the Service Account, since I'm not the owner of the project, I got the following message: 1创建服务帐户时,由于我不是项目所有者,因此收到以下消息:
The service account could not be added to this project's permissions because you are not an owner of the project. 无法将服务帐户添加到该项目的权限中,因为您不是该项目的所有者。 To give the service account access to this project, ask a project owner to add it to the project permissions. 要授予服务帐户对此项目的访问权限,请项目所有者将其添加到项目权限中。
I wonder whether this has something to do with the absence of a client_secret
property in the automatically downloaded JSON. 我想知道这是否与自动下载的JSON中缺少client_secret
属性有关。
I don't think the dev server will expose the Task Queue API, do your tests against a separate test project that you create. 我认为开发服务器不会公开Task Queue API,而是针对您创建的单独的测试项目进行测试。
You can use the JSON from the service account to connect to the Task Queue API, see service accounts documentation. 您可以使用服务帐户中的JSON连接到Task Queue API,请参阅服务帐户文档。
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json")) .createScoped(Collections.singleton(" https://www.googleapis.com/auth/taskqueue ")); GoogleCredential凭据= GoogleCredential.fromStream(new FileInputStream(“ MyProject-1234.json”)).createScoped(Collections.singleton(“ https://www.googleapis.com/auth/taskqueue ”));
Taskqueue taskQueue = new Taskqueue.Builder( httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME) .setTaskqueueRequestInitializer(new TaskqueueRequestInitializer() { @Override public void initializeTaskqueueRequest(TaskqueueRequest request) { request.setPrettyPrint(true); } }).build(); Taskqueue taskQueue = new Taskqueue.Builder(httpTransport,JSON_FACTORY,凭据).setApplicationName(APPLICATION_NAME).setTaskqueueRequestInitializer(new TaskqueueRequestInitializer(){ );
// get queue //获取队列
com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue); com.google.api.services.taskqueue.model.TaskQueue queue = getQueue(taskQueue); System.out.println(queue); System.out.println(queue);
// lease, execute and delete tasks //租用,执行和删除任务
Tasks tasks = getLeasedTasks(taskQueue); 任务task = getLeasedTasks(taskQueue);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.