1

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="User is notauthorized." の応答を返します。

ただし、応答の users フィールドが実際に ["{{MY_APP_ID}}@appspot.gserviceaccount.com"] であるため、正しいユーザーを認識しているようです。

私は何を間違っていますか?App Engine アプリをアプリ独自の権限で Google クラウド プリントに印刷する方法はありますか?

4

3 に答える 3

2

上記のコード例では、承認されていないエラー メッセージをスローしているヘッダーで認証文字列を送信しません。次の変更と 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();
        }

    }
于 2016-12-06T04:30:59.247 に答える
0

TL;DR- https://stackoverflow.com/a/19951913/929444

ロングバージョン~

Google クラウド プリント プリンタへのアクセス許可を他のユーザーに付与すると、Google はそのユーザーに承認を求めるメールを送信します。もちろん、Google の UI はそれをまったく示していません。AppEngine アプリのユーザーは電子メールのリンクを押すことができないため、私が望んでいたことを行う簡単な方法はありません。

上記のリンクで提案されている回避策は、AppEngine アプリと別の通常のユーザーを含む Google グループを作成することです。誰かがグループを追加するたびに、通常のユーザーが承認し、アプリにはグループの一部として暗黙的にアクセス許可が付与されます。

于 2014-04-20T14:20:14.583 に答える