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
Register the Google Cloud Print printer by referring to the following. https://www.google.com/cloudprint/learn/
Go to GoogleCloudPratform and register your credentials.
https://console.cloud.google.com/
Select [APIs and Services] → [Credentials] → [Create Credentials] → [OAuth Client ID]
This time, it's a trial, so select [Other]
Get client ID
and client secret
(will be used later)
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]
Get the code
(will be used later)
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.
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