0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google クラウドプリントをシェルスクリプトで

Posted at

Google クラウドプリントをcurlでPDFをアップロードし,任意のプリンタで印刷 or Google ドライブに保存する.

やりかた


f=ファイルパス

bn=$(basename $f)

curl -H "Authorization: OAuth アクセストークン" -F title="$bn" -F ticket='{"version": "1.0", "print": {}}' -F contentType="application/pdf" -F printerid="プリンタID" -F "content=@$f;type=application/pdf" "https://www.google.com/cloudprint/submit"

トークンの取り方

トークンを取得する方法は以下が一番見やすかった.
https://qiita.com/shin1ogawa/items/49a076f62e5f17f18fe5

# !/bin/bash

CLIENT_ID=ほげ.apps.googleusercontent.com
CLIENT_SECRET=ふーばー
REDIRECT_URI=http%3A%2F%2Flocalhost%2F
SCOPE=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudprint

echo "https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=$CLIENT_ID&redirect_uri=$REDIRECT_URI&scope=$SCOPE&access_type=offline"

エンドポイントであるhttp://localhost/ほげに飛ばされるので,ほげのコードを以下で入れて,再度叩く.

# !/bin/bash

CLIENT_ID=ほげほげ.apps.googleusercontent.com
CLIENT_SECRET=ふーばー
REDIRECT_URI=http%3A%2F%2Flocalhost%2F
SCOPE=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloudprint
# 以下に入れる
AUTHORIZATION_CODE=ほげ

curl --data "code=$AUTHORIZATION_CODE" --data "client_id=$CLIENT_ID" --data "client_secret=$CLIENT_SECRET" --data "redirect_uri=$REDIRECT_URI" --data "grant_type=authorization_code" --data "access_type=offline" https://www.googleapis.com/oauth2/v4/token

これでアクセストークンが貰える.

macOSのAutomatorで遊ぶ

PDF プリントプラグインにシェルスクリプト実行をぶら下げて,以下を入れる.

for f in "$@"
do

bn=$(basename $f)

curl -H "Authorization: OAuth アクセストークン" -F title="$bn" -F ticket='{"version": "1.0", "print": {}}' -F contentType="application/pdf" -F printerid="__google__docs" -F "content=@$f;type=application/pdf" "https://www.google.com/cloudprint/submit"



done

シェルは/bin/bash,また受け取り方は引数としてを選択する.
たったこれだけで,共有プリンタのできあがりである.

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?