Print PDF using Google Cloud Print. (GoogleAPI)

What is Google Cloud Print?

A service that allows you to register the printer set on your device in your Google account and print from another device via the cloud.

--Settings required for printing --Getting a Google account --Install Chrome browser --Printer registration --Settings required to call from Google API --Getting the Google API authentication key

Process flow

  1. Acquisition of refresh token (not necessary once acquired)
  2. Obtaining an access token
  3. Operation of Google Cloud Print (Getting a list of printers, executing printing by specifying a printer ID)

Advance preparation

Register the Google Cloud Print printer by referring to the following. https://www.google.com/cloudprint/learn/

Get Google API client ID and secret

Go to GoogleCloudPratform and register your credentials.

https://console.cloud.google.com/

Select [APIs and Services] → [Credentials] → [Create Credentials] → [OAuth Client ID] image.png

This time, it's a trial, so select [Other] image.png

Get client ID and client secret (will be used later) image.png

Get permission to Google Cloud Print and code to set in the application

Set the URL of the obtained client ID and access the browser directly https://accounts.google.com/o/oauth2/auth?redirect_uri=oob&response_type=code&client_id=<ここに上記で取得したクライアントIDを指定>&scope=https://www.googleapis.com/auth/cloudprint

Select [Allow] image.png image.png

Get the code (will be used later) image.png

Automatic printing with Google API

This time, I will try to execute it using Java Unirest. Pick up and describe only important processes

getRefreshToken


		HttpResponse<JsonNode> res = Unirest.post("https://www.googleapis.com/oauth2/v3/token")
				.field("code", code) //Set the code obtained in advance
				.field("client_id", client_id)  //Set the client ID obtained in advance
				.field("client_secret", client_secret)  //Set the client secret obtained in advance
				.field("grant_type", "authorization_code")
				.field("redirect_uri", "oob")
				.asJson();

Get a refresh token. Once you use the code, you can no longer use it, so if you don't know the refresh token, you need to get the code in advance.

getAccessToken


		HttpResponse<JsonNode> res = Unirest.post("https://www.googleapis.com/oauth2/v3/token")
				.field("client_id", client_id)  //Set the client ID obtained in advance
				.field("client_secret", client_secret)  //Set the client secret obtained in advance
				.field("grant_type", "refresh_token")
				.field("refresh_token", refresh_token)  //Set the refresh token acquired by getRefreshToken
				.asJson();

Get an access token. Get an access token from the refresh token each time. Configure to get an access token each time you access the printer.

getPrinters


		HttpResponse<JsonNode> res = Unirest.get("https://www.google.com/cloudprint/search")
				.header("Authorization", token_type + " " + access_token)
				.asJson();

Get the list of printers set in Google Cloud Print.

submitFile


		HttpResponse<JsonNode> res = Unirest.post("https://www.google.com/cloudprint/submit")
				.header("Authorization", token_type + " " + access_token)
				.field("printerid", printerid)  //Set Printer ID: The default Google Drive is[__google__docs]is.
				.field("title", file.getName())
				.field("ticket", gson.toJson(new TicketBean()))
				.field("content", file)
				.asJson();

Make a print request for a local PDF file to Google Cloud Print. file is of type java.io.File.

You can print by performing these processes in order.

Finally

For example, implement dynamic PDF creation processing on the server side and print to printers on multiple client sides. It will be a mechanism that can be easily realized.

It's difficult now because I need to register the printer in my account Eventually, the day may come when faxes are no longer needed.

Recommended Posts

Print PDF using Google Cloud Print. (GoogleAPI)
Try using Python with Google Cloud Functions
Speech transcription procedure using Google Cloud Speech API
I tried using the Google Cloud Vision API
[Google Cloud Platform] Use Google Cloud API using API Client Library
Speech transcription procedure using Python and Google Cloud Speech API
Try to determine food photos using Google Cloud Vision API
Let's publish the super resolution API using Google Cloud Platform
Cloud image prediction using convLSTM
What is Google Cloud Dataflow?
Stream speech recognition using Google Cloud Speech gRPC API on python3 on Mac!