2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LINE WORKS メール取得 API の使用方法

LINE WORKS の API を活用して、特定のメールを取得する方法について解説します。本記事では、Google Colab を使用して、API を簡単に実行するためのスクリプトを提供します。

概要

LINE WORKS のメール取得 API を利用することで、以下のような情報を取得できます:

  • 特定のメールの詳細情報(送信者、受信者、件名、本文など)
  • 添付ファイルやインライン画像のデータ

必要な準備

1. アクセストークンの取得

メール取得 API を使用するには、User Account 認証によるアクセストークンが必要です。アクセストークンの取得方法については、以下の記事をご参照ください:

注意:アクセストークンには mail または mail.read スコープが含まれている必要があります。

2. Google Colab の準備

以下のリンクから Google Colab ノートブックにアクセスして、サンプル コードをそのまま試すことができます:

Google Colab: メール取得サンプル

サンプル コード

以下は、Google Colab 上で動作する Python スクリプトのサンプルです。

# 必要なライブラリをインストール
!pip install requests

# ライブラリのインポート
import requests
import json

# 設定変数
ACCESS_TOKEN = "your_access_token"  # @param {type:"string"}
USER_ID = "me"  # @param {type:"string"} "me" または LINE WORKS のユーザー ID
MAIL_ID = "12345"  # @param {type:"string"} 取得したいメールの ID

# API サーバー
API_SERVER = "https://www.worksapis.com"
API_ENDPOINT = f"{API_SERVER}/v1.0/users/{USER_ID}/mail/{MAIL_ID}"

# メール取得 API の呼び出し
def get_mail(access_token):
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
    }
    response = requests.get(API_ENDPOINT, headers=headers)

    # レスポンスに基づく出力
    if response.status_code == 200:
        print("✅ メールを取得しました!")
        result = response.json()
        print(json.dumps(result, indent=4, ensure_ascii=False))
    else:
        print(f"❌ メールの取得に失敗しました。ステータスコード: {response.status_code}")
        print(response.text)

# メール取得 API を実行
get_mail(ACCESS_TOKEN)

入力例

  • ACCESS_TOKEN: User Account 認証によるアクセストークン
  • USER_ID: "me" または LINE WORKS のユーザー ID
  • MAIL_ID: メールの ID(フォルダ内のメール一覧 API で取得可能)

実行結果

成功時

✅ メールを取得しました!
{
    "attachments": [],
    "mail": {
        "mailId": 12345,
        "folderId": 0,
        "status": 2097157,
        "from": {
            "name": "送信者名",
            "email": "sender@example.com"
        },
        "to": [
            {
                "name": "受信者名",
                "email": "receiver@example.com"
            }
        ],
        "subject": "テストメール",
        "body": "これはメールの本文です。",
        "receivedTime": "2024-12-15T00:00:00+09:00"
    }
}

失敗時(例: スコープ不足)

❌ メールの取得に失敗しました。ステータスコード: 403
{"code": "ACCESS_DENIED", "description": "Access is denied for userId."}

注意事項

  1. アクセストークンのスコープ

    • アクセストークンには mail または mail.read スコープが含まれている必要があります。
  2. メール ID の取得

  3. インライン画像の処理

    • メール本文内にインライン画像が含まれている場合、API のレスポンスに含まれる attachments フィールドを参照してください。

LINE WORKS メール取得 API を活用して、メール管理を効率化しましょう!

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?