Recently printers connected to the Internet became common. As some of my web services required printing in the background process, I have researched Google Cloud Print API as one of printing measures.
(0) Get a client ID and a client secret in Google API Console
(1) Show an authorization page and get a grant from the user.
- Specify "https://www.googleapis.com/auth/cloudprint" in the scope parameter.
- An authorization URL is following format.
https://accounts.google.com/o/oauth2/v2/auth? redirect_uri={registered redirect URI}& response_type=code& client_id={issued client ID}& scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudprint& access_type=offline
(2) Receive an authorization code in the redirect URI and exchange it for an access token.
- The browser will be redirected to the redirect URI with an authorization code in the code parameter.
- Send the authorization code extracted from the URI to the token endpoint and get an access token.
Request POST https://www.googleapis.com/oauth2/v4/token Content-Type: application/x-www-form-urlencoded code={authorization code}& client_id={issued client ID}& client_secret={issued client secret}& redirect_uri={registered redirect URI}& grant_type=authorization_code Response { "access_token": {access token}, "token_type": "Bearer", "expires_in": 3600, "refresh_token": {refresh token} }
(3) Call the printer search API and get the printer ID.
Request GET https://www.google.com/cloudprint/search Authorization: OAuth {access token} Response { "printers": [ { "id": {printer ID}, "displayName": {display name of the printer}, ... }] ... }
(4) Call a printer job submit API and execute printing
- e.g.) a request to print the web page by specifying a URL
POST https://www.google.com/cloudprint/submit Content-Type: application/x-www-form-urlencoded printerId={printer ID}& title={print title}& contentType=url& content={URL to print}& ticket={ticket object}
- Printing options such as color and copy number can be configured in a ticket object
- Reference:https://developers.google.com/cloud-print/docs/cdd#cjt
- e.g.) Print 1 copy in black and white.
{"version":"1.0","print":{"vendor_ticket_item":[],"color":{"type":"STANDARD_MONOCHROME"},"copies":{"copies":1}}}