我正在尝试从 App Engine 应用 (Java) 向 Google Cloud Print 提交新的打印作业。
在打印机的 Google 云打印设置中,我将“可以打印”权限设置为 {{MY_APP_ID}}@appspot.gserviceaccount.com
然后,从 App Engine 应用程序运行以下代码 -
AppIdentityCredential credential = new AppIdentityCredential(
Arrays.asList("https://www.googleapis.com/auth/cloudprint"));
HttpRequestFactory requestFactory =
new UrlFetchTransport().createRequestFactory(credential);
Map<String, String> params = new LinkedHashMap<String, String>();
params.put("printerid", "{{PRINTER_ID}}");
params.put("title", "Title");
params.put("ticket", "{\"version\":\"1.0\", \"print\":{}}");
params.put("content", "<!DOCTYPE html>\n<html>\n<body>\nHello world!\n</body>\n</html>");
params.put("contentType", "text/html");
HttpRequest httpRequest = requestFactory.buildPostRequest(
new GenericUrl("https://www.google.com/cloudprint/submit"),
new UrlEncodedContent(params));
HttpResponse httpResponse = httpRequest.execute();
try {
System.out.println(httpResponse.parseAsString());
} finally {
httpResponse.ignore();
}
Google Cloud Print 的 API 返回成功=false 和 message="User is not authorized." 的响应。
但是,它似乎确实识别出正确的用户,因为响应的用户字段确实是 ["{{MY_APP_ID}}@appspot.gserviceaccount.com"]。
我究竟做错了什么?有没有办法让 App Engine 应用程序使用应用程序自己的权限打印到 Google 云打印?