gpt4 book ai didi

java - 从 App Engine 应用程序向 Google 云打印提交打印作业

转载 作者:行者123 更新时间:2023-11-30 03:55:58 24 4
gpt4 key购买 nike

我正在尝试从 App Engine 应用 (Java) 向 Google 云打印提交新的打印作业。

在打印机的 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 云打印的 API 返回一条响应,其中包含 success=false 和 message="用户未授权。"

但是,它似乎确实识别了正确的用户,因为响应的用户字段确实是 ["{{MY_APP_ID}}@appspot.gserviceaccount.com"]。

我做错了什么?有没有办法让 App Engine 应用程序使用该应用程序自己的权限打印到 Google 云打印?

最佳答案

上面的示例代码不会在 header 中发送身份验证字符串,从而引发未经授权的错误消息 - 对 Oauth 2 的以下修改和更新适用于与服务帐户共享的打印机:

    private static final String APPLICATION_NAME = "my-cloud-print";
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private static final String KEY_FILE_LOCATION = "file.p12";
private static final String SERVICE_ACCOUNT_EMAIL = "12345@developer.gserviceaccount.com";

public static void main( String args[] ){

try{

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

//service login
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountPrivateKeyFromP12File(new File(KEY_FILE_LOCATION))
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/cloudprint"))
.build();

//update to fetch token
credential.refreshToken();
String accessToken = credential.getAccessToken();

Map<String, String> params = new LinkedHashMap<String, String>();
params.put("printerid", "0000000-f960-1d6b-077f-750e5b45fbdd");
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");

HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
HttpRequest httpRequest = requestFactory.buildPostRequest(
new GenericUrl("https://www.google.com/cloudprint/submit"),
new UrlEncodedContent(params)
);

//important bit - must send the auth token to print service
//after you process the invitation from the share
//see http://stackoverflow.com/questions/19767752
httpRequest.getHeaders().set("Authorization", Arrays.asList( ("Bearer " + accessToken).split(",") ));

HttpResponse httpResponse = httpRequest.execute();
try {
System.out.println(httpResponse.parseAsString());
} finally {
httpResponse.ignore();
}
} catch( Exception ex ){
ex.printStackTrace();
}

}

关于java - 从 App Engine 应用程序向 Google 云打印提交打印作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23177004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com