Skip to content

Instantly share code, notes, and snippets.

@ohga
Created December 11, 2017 09:38
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohga/7097677251152fd03268f11c9d36f756 to your computer and use it in GitHub Desktop.
Save ohga/7097677251152fd03268f11c9d36f756 to your computer and use it in GitHub Desktop.

がんばって curl コマンドだけで Google Drive にファイル上げるメモ

基本は、PythonでGoogle Drive API v3を利用して画像のアップロード の準備のトコとGoogle APIのAccess Tokenをお手軽に取得する、の合せ技

やること6. でリフレッシュトークンが貰えれば、あとは 8. を繰替えすだけで良い。(1.7. は一回だけやればいいハズ)

ただ、 リフレッシュトークンがあると、Google Drive に対してやりたいほうだいになるので、リフレッシュトークンをクライアントに配るようは運用は考えづらい。善意で成り立っているなら、専用のアカウント作って運用、みたいなのはあるのか?

やるならファイルのアップロードを受けてるような中継用のサーバアプリを用意して、こいつで送信する、とか?(それならコマンドなんか使わずに、いろんな言語でSDKが出てるのでそれを使うから、やっぱ緊急用か、これは。)

やること

  1. 開発アカウントを作る(とりあえず既存の Google のアカウントでも良い)
  2. 開発者コンソールから空のアプリを作る
  3. アプリを承認してもらうための情報を用意して、アプリの OAuth2 の client_id/client_secret を作る
  4. client_id を使用して、アプリに対して Google Drive の操作の許可を得る為のページの URL を作成する
  5. Google Drive の操作を許可していいアカウント(とりあえず既存の Google のアカウントでも良い)で、URL にアクセスして Google Drive の操作を許可してもらい 認証コード を貰う
  6. client_id/client_secret認証コード を使って access_token/refresh_token を払いだしてもらう
  7. jq のインストール
  8. access_token/refresh_token を使ってファイルをアップロード

1. 開発アカウントを作る

Google アカウントでログインしておく。

2. 開発者コンソールから空のアプリを作る

https://console.developers.google.com/start/api?id=drive にアクセスして新規でアプリを作成する。
とりあえず、作成後の青いボタンは押さなくていい。(認証情報には進まなくていい)

3. アプリを承認してもらうための情報を用意して、アプリのOAuth2のclient_id/client_secretを作る

https://console.developers.google.com/apis/credentials/consent にアクセスして、サービス名 を入力して「このアプリに許可を与えますか?」的な画面を作成する。
次に https://console.developers.google.com/apis/credentials にアクセスして、認証情報を作成ボタンを押下して「OAuth クライアントID」 を選択する。  

アプリケーションの種類は「その他」にして、適当な名前を入力して作成。
クライアントIDとクライアントシークレットをどっかにメモっておきつつ、コンソールで以下を実行して環境変数に乗せておく。(説明用)

$ export CLIENT_ID=999999999999-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com 
$ export CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxx"
$ export REDIRECT_URI=urn:ietf:wg:oauth:2.0:oob
$ export SCOPE=https://www.googleapis.com/auth/drive.file

4. client_id を使用して、アプリに対して Google Drive の操作の許可を得る為のページのURLを作成する

ターミナルで以下を実行。

$ 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"

5. Google Drive の操作を許可していいアカウントで、URLにアクセスして Google Drive の操作を許可してもらい、認証コードを貰う

前述のURLにアクセスして、許可を出す。認証コードが払い出されるのでメモしつつ コンソールで以下を実行して環境変数に乗せておく。(説明用)

$ export AUTHORIZATION_CODE="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

6. client_id/client_secret と認証コードを使って access_token/refresh_token を払いだしてもらう

ターミナルで以下を実行。

$ 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 -s

以下のような json が応答される

{
 "access_token": "xxxxxxxxxx",
 "token_type": "Bearer",
 "expires_in": 3600,
 "refresh_token": "xxxxxxxxxx"
}

とりあえず、 refresh_token だけ環境変数に乗せておく。

$ export REFRESH_TOKEN="xxxxxxxxxx"

7. jq のインストール

jq コマンドを入れておく。(ひよった。。)

$ sudo curl -o /usr/local/bin/jq -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 && sudo chmod +x /usr/local/bin/jq

8. access_token/refresh_token を使ってファイルをアップロード

リフレッシュトークンを使って、アクセストークンを取得(して、環境変数に入れておく)

$ export ACCESS_TOKEN=`curl --data "refresh_token=$REFRESH_TOKEN" --data "client_id=$CLIENT_ID" --data "client_secret=$CLIENT_SECRET" --data "grant_type=refresh_token" https://www.googleapis.com/oauth2/v4/token -s | jq  -r .access_token`

アップロードするファイル('/tmp/shufout_nnnnnnnnnnnnnnnnnnnn.hcpe')を用意。
それを元にメタデータを用意

例(/tmp/meta.json):

{
  "title": "shufout_nnnnnnnnnnnnnnnnnnnn.hcpe",
  "mimeType": "application/octet-stream",
  "description": "Uploaded From Curl"
}

file --mime-type -b /tmp/shufout_nnnnnnnnnnnnnnnnnnnn.hcpe で mimeType,
basename /tmp/shufout_nnnnnnnnnnnnnnnnnnnn.hcpe で title

以下のコマンドを実行。

$ curl -X POST "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=$ACCESS_TOKEN" \
 -H "Content-Type: multipart/related" \
 -F "metadata=@/tmp/meta.json;type=application/json;charset=UTF-8" \
 -F "file=@/tmp/shufout_nnnnnnnnnnnnnnnnnnnn.hcpe;type=application/octet-stream" 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment