LoginSignup
1

posted at

updated at

Organization

LINE WORKS APIのためのコマンドラインツールを作った

はじめに

LINE WORKS APIを簡単に実行・検証できるように、コマンドラインツールを作成した。

これを使うことでアクセストークンの取得や管理を容易にでき、連携システムの開発を進めやすくなることが期待できる。

そのコマンドの使い方についてまとめる。

注意事項

個人として作成したツールであり、公式に提供・サポートされているツールではありません。不具合報告・質問等は該当リポジトリのIssuesや本記事のコメントにてお知らせください。

Repository

Install

このリポジトリのReleaseに、バージョンごとに実行ファイルがアップロードされている。環境に合わせてダウンロード・解凍する。

  • macOS (Intel) : lineworks-cli_x.x.x_Darwin_x86_64.tar.gz
  • macOS (M1/M2) : lineworks-cli_x.x.x_Darwin_arm64.tar.gz
  • Windows (64bit) : lineworks-cli_x.x.x_Windows_x86_64.tar.gz
  • Windows (Arm) : lineworks-cli_x.x.x_Windows_arm64.tar.gz
  • Linux (Arm) : lineworks-cli_x.x.x_Linux_arm64.tar.gz
  • Linux (Intel 64bit) : lineworks-cli_x.x.x_Linux_x86_64.tar.gz
  • Linux (Intel 32bit) : lineworks-cli_x.x.x_Linux_i386.tar.gz

実行ファイルを任意の場所に格納

  • Linux/macOSの場合 : lineworks ファイル
  • Windowsの場合 : lineworks.exe ファイル

※注意
実行する際に、セキュリティ設定によっては実行がブロックされる場合があります。必要に応じてOSのセキュリティ設定をご確認ください。

できること


使い方

以下、コマンドサンプルは、Linux/macOSはbash、WindowsはPowerShellを前提として記載する。

設定

まずは、アクセストークン取得のための各認証情報の設定を行う。

設定は profile ごとに設定することができ、コマンド実行時に指定する profile パラメータで設定を切り替えることができる。

設定済みの profile については、以下のコマンドで一覧を参照可能。

Linux/macOS
./lineworks list-profiles
Windows
.\lineworks.exe list-profiles

クライアント認証情報の設定

OAuthのクライアント認証情報 (Client ID, Client Secret) を設定する。
profile は設定名となる。任意の名前を指定する。

Linux/macOS
./lineworks configure set-client \
    --client-id "client_id" \
    --client-secret "client_secret" \
    --profile "profile"
Windows
.\lineworks.exe configure set-client `
    --client-id "client_id" `
    --client-secret "client_secret" `
    --profile "profile"

設定内容は以下で確認できる。

Linux/macOS
./lineworks configure get-client --profile "profile"
Windows
.\lineworks.exe configure get-client --profile "profile"

Developer ConsoleでRedirect URLを設定

※ User Account認証を利用する場合のみ

Developer Consoleのアプリ設定に、事前にRedirect URLを設定する。

Redirect URLは以下で取得できる。

Linux/macOS
./lineworks configure get-redirect-url --profile "profile"
Windows
.\lineworks.exe configure get-redirect-url --profile "profile"

取得した値を、Developer Consoleのアプリ設定の [Redirect URL] に追加する。


サービスアカウント情報の設定

※ Service Account認証を利用する場合のみ

サービスアカウント情報を設定する。
※ Private Keyは、ファイルパスを指定することで設定。

Linux/macOS
./lineworks configure set-service-account \
    --service-account-id "serivce_account_id" \
    --private-key-file "private_key_file_path" \
    --profile "profile"
Windows
.\lineworks.exe configure set-service-account `
    --service-account-id "serivce_account_id" `
    --private-key-file "private_key_file_path" `
    --profile "profile"

設定内容は以下で確認できる。

Linux/macOS
./lineworks configure get-service-account --profile "profile"
Windows
.\lineworks.exe configure get-service-account --profile "profile"

アクセストークン実行

User Account認証

scopes に複数設定する場合はカンマ区切りで指定。

Linux/macOS
./lineworks auth user-account --scopes "scopes" --profile "profile"
Windows
.\lineworks.exe auth user-account --scopes "scopes" --profile "profile"

実行すると、ブラウザが立ち上がり、認証画面が表示されるため、Sign-onする。

認証に成功すると、取得完了。後述の「アクセストークンの参照」で値を取得

Service Account認証

scopes に複数設定する場合はカンマ区切りで指定。

Linux/macOS
./lineworks auth service-account --scopes "scopes" --profile "profile"
Windows
.\lineworks.exe auth service-account --scopes "scopes" --profile "profile"

取得完了。後述の「アクセストークンの参照」で値を取得

アクセストークンの参照

取得したアクセストークンは以下のコマンドで参照する。

Linux/macOS
./lineworks auth get-access-token --profile "profile"
Windows
.\lineworks auth get-access-token --profile "profile"

APIリクエスト

取得したアクセストークンを使ってAPIを叩く。
このコマンドにはHTTPリクエスト機能は実装されていないため、任意のHTTPクライアントツールでAPIリクエストを実行する。アクセストークンはBearer認証としてAuthorization headerに指定する。

参考: https://developers.worksmobile.com/jp/reference/api-request?lang=ja

cURLの場合 (Linux/macOS)

アクセストークンを以下のように環境変数に格納。

TOKEN=`./lineworks auth get-access-token --profile "profile"

cURLコマンドでリクエストする (例. ユーザー取得)
※ User Account認証にて取得した、scopes に user.read が含まれるアクセストークン。

curl -X GET https://www.worksapis.com/v1.0/users/me \
    -H "Content-Type:application/json" \
    -H "Authorization: Bearer $TOKEN"

改善予定

  • Refresh Tokenを使ったトークン再発行の実装
  • Access Tokenの有効期限チェック
  • APIリクエスト機能

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
What you can do with signing up
1