created: 2023-05-24T07:38:54.499Z

gmailctl で認証するところまでを細かく

認証情報を設定してない状態で gmailctl init をたたくとこのようなメッセージが出る。メッセージは丁寧にかかれているが、UI への案内が英語表記での UI のものになっている。GCP のコンソールが日本語設定の場合の操作をメモっておく。

$ gmailctl init
The credentials are not initialized.

To do so, head to https://console.developers.google.com

1. Create a new project if you don't have one.
1. Go to 'Enable API and services', search for Gmail and enable it.
2. Go to 'OAuth consent screen'.
    2a. If your account is managed by an organization, you have to
        select 'Internal' as 'User Type'. For individual accounts
        select 'External'.
    2b. Set an application name (e.g. 'gmailctl').
    2c. Use your email for 'User support email' and 'Developer
        contact information'. Save and continue.
    3c. Select 'Add or remove scopes' and add:
        * https://www.googleapis.com/auth/gmail.labels
        * https://www.googleapis.com/auth/gmail.settings.basic
    3d. Save and continue until you're back to the dashboard.
3. You now have a choice. You can either:
    * Click on 'Publish App' and avoid 'Submitting for
      verification'. This will result in scary confirmation
      screens or error messages when you authorize gmailctl with
      your account (but for some users it works), OR
    * You could add your email as 'Test user' and keep the app in
      'Testing' mode. In this case everything will work, but
      you'll have to login and confirm the access every week (token
      expiration).
4.  Go to Credentials on the left.
    4a. Click 'Create credentials'.
    4b. Select 'OAuth client ID'.
    4c. Select 'Desktop app' as 'Application type' and give it a name.
    4d. Create.
5. Download the credentials file into "$HOME/.gmailctl/credentials.json" and execute the 'init'
   command again.

Documentation about Gmail API authorization can be found
at: https://developers.google.com/gmail/api/auth/about-auth

前庭として仕事で使う(GoogleWorkspace にひもづいた)Google アカウントに紐づけた状態で gmailctl を使う。

管理画面から所属組織の配下に適当な名前をつけてプロジェクトを作成

横着して既存の GCP プロジェクトにぶら下げない方がよい。プロジェクトは思ったより簡単に作れる。gmailctl-oauth みたいな名前になっていれば後からわかるかなと思う。

OAuth クライアント ID を作成するために、同意画面を設定

  • 内部(所属組織の中というか自分のみで使うので)
    • 連絡先のメールアドレスを 2 つ埋める
    • アイコンとか承認済みドメインは入れなくても設定ボタンが押せる
  • スコープを追加。検索してチェックを入れて 更新 ボタン
    • https://www.googleapis.com/auth/gmail.labels
    • https://www.googleapis.com/auth/gmail.settings.basic

Gmail の API を有効にして、コンソールからクライアント ID を払い出す。

  • 認証情報
    • 認証情報を作成
      • OAuth クライアント ID の作成
        • デスクトップアプリ

credentials.json

作成 ボタンを押すと json をダウンロードできるのでこれを ~/.gmailctl/credentials.json におく

$ mv ~/Downloads/client_secret_xxxx.apps.googleusercontent.com.json ~/etc/gmailctl/work1/credentials.json
# symlinkでも動く
$ ln -s ~/etc/gmailctl/work1/credentials.json ~/.gmailctl/credentials.json

gmailctl init

もう一回 gmailctl init を叩くと URL が出力されるので、Google アカウントでログインできているブラウザでアクセスして権限を許可すれば OK。

$ gmailctl init
missing or invalid cached token: open ~/.gmailctl/token.json: no such file or directory

Go to the following link in your browser and authorize gmailctl:

https://accounts.google.com/o/oauth2/....xxxx......Y%3D (長い)

NOTE that gmailctl runs a webserver on your local machine to
collect the token as returned from Google. This only runs until
the token is saved. If your browser is on another machine
without access to the local network, this will not work.
Saving credential file to ~/.gmailctl/token.json

You have correctly configured gmailctl to use Gmail APIs.

ブラウザでこれが表示されればおしまい。

Successfully authenticated with Google OAuth2.
You may now close this page and return to the terminal.

動作確認

ためしに gmailctl download してみると認証されているか動作確認できる。

$ gmailctl download | head -20
// Auto-imported filters by 'gmailctl download'.
//
// WARNING: This functionality is experimental. Before making any
// changes, check that no diff is detected with the remote filters by
// using the 'diff' command.

// Uncomment if you want to use the standard library.
// local lib = import 'gmailctl.libsonnet';
{
  version: "v1alpha3",
  author: {
    name: "YOUR NAME HERE (auto imported)",
    email: "your-email@gmail.com"
  },
  // Note: labels management is optional. If you prefer to use the
  // GMail interface to add and remove labels, you can safely remove
  // this section of the config.
  labels: [
    {
      name: "warn,crit",

jsonnet でフィルタとラベルを操作できるのはとてもいい。

みんなのデータ構造
[ad] みんなのデータ構造
Pat Morin, 堀江 慧 (単行本(ソフトカバー))