MeWrite Docs

MissingAuthenticationToken: Missing Authentication Token

概要

API GatewayやAWSサービスで認証トークンが提供されていない場合に発生するエラーです。APIエンドポイントのURL間違い、認証ヘッダーの欠落、またはIAM認証が必要なリソースへの未認証アクセスが原因です。

エラーメッセージ

MissingAuthenticationToken: Missing Authentication Token
{
    "message": "Missing Authentication Token"
}

原因

このエラーは以下の原因で発生します:

  1. APIエンドポイントのURL間違い: 存在しないパスにアクセス
  2. 認証ヘッダーの欠落: Authorization ヘッダーがない
  3. HTTPメソッドの間違い: GET/POSTの指定ミス
  4. ステージ名の欠落: API Gateway のステージ名がURLにない
  5. IAM認証が必要: IAMシグネチャが必要なAPIに未認証でアクセス

解決策

1. APIエンドポイントのURLを確認

1
2
3
4
5
6
7
8
# 正しいURL形式
# https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/{path}

# NG: ステージ名がない
curl https://abc123.execute-api.ap-northeast-1.amazonaws.com/users

# OK: ステージ名を含める
curl https://abc123.execute-api.ap-northeast-1.amazonaws.com/prod/users

2. HTTPメソッドを確認

1
2
3
4
5
6
# API Gatewayで定義されたメソッドを使用
# NG: POSTで定義されているのにGETでアクセス
curl https://api.example.com/prod/users

# OK: 正しいメソッドを使用
curl -X POST https://api.example.com/prod/users

3. IAM認証を追加

1
2
3
4
5
6
# AWS Signature Version 4 で署名
# aws-cli を使用
aws apigateway test-invoke-method \
    --rest-api-id abc123 \
    --resource-id xyz789 \
    --http-method GET
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# Python (requests + aws-requests-auth)
from aws_requests_auth.aws_auth import AWSRequestsAuth
import requests

auth = AWSRequestsAuth(
    aws_access_key='AKIAIOSFODNN7EXAMPLE',
    aws_secret_access_key='wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
    aws_host='abc123.execute-api.ap-northeast-1.amazonaws.com',
    aws_region='ap-northeast-1',
    aws_service='execute-api'
)

response = requests.get(
    'https://abc123.execute-api.ap-northeast-1.amazonaws.com/prod/users',
    auth=auth
)

4. API Keyを追加

1
2
3
# x-api-key ヘッダーを追加
curl -H "x-api-key: your-api-key" \
    https://abc123.execute-api.ap-northeast-1.amazonaws.com/prod/users

5. Cognito認証を追加

1
2
3
# Cognito トークンを Authorization ヘッダーに追加
curl -H "Authorization: Bearer eyJraWQiOiJ..." \
    https://abc123.execute-api.ap-northeast-1.amazonaws.com/prod/users

よくある間違い

  • API Gateway のステージ名(dev, prod等)をURLに含めていない
  • カスタムドメインでパスマッピングが設定されていない
  • CORSプリフライトリクエスト(OPTIONS)が拒否されている
  • Lambda関数のURLを直接呼び出そうとしている

デバッグ手順

1
2
3
4
5
6
7
8
# 1. API Gateway コンソールでエンドポイントを確認
# 2. ステージ → ステージ名 → 呼び出しURL

# 3. リソースとメソッドを確認
aws apigateway get-resources --rest-api-id abc123

# 4. デプロイされているか確認
aws apigateway get-deployments --rest-api-id abc123

関連エラー

参考リンク

関連エラー

AWS の他のエラー

最終更新: 2025-12-16