Skip to content

Azure Graph API 驗證與呼叫

使用 Azure AD OAuth2 ROPC 流程取得 Access Token,再呼叫 Microsoft Graph API 的完整流程與實際參數。

概述

Microsoft Graph API 是存取 Microsoft 365 服務(郵件、行事曆、使用者資訊等)的統一入口。呼叫前需先向 Azure AD 取得 OAuth2 Access Token。

本文記錄使用 Resource Owner Password Credentials(ROPC) 流程的操作方式:直接以使用者帳號與密碼換取 Token,不需要互動式瀏覽器授權。ROPC 流程適合自動化腳本與服務帳號情境,但需要注意安全性(Token 有效期短,credentials 不應寫進版本控制)。

注意: ROPC 流程需要 Azure AD App Registration 啟用此授權方式,且帳號不能啟用 MFA。新專案建議改用 Client Credentials(App-only)流程以避免 MFA 限制。

核心內容

Step 1:取得 Access Token

向 Azure AD Token Endpoint 發送 POST 請求:

POST https://login.microsoftonline.com/{TENANT_ID}/oauth2/token
Content-Type: application/x-www-form-urlencoded

必填參數:

參數說明
grant_type固定填 password(ROPC 流程)
client_idApp Registration 的 Application (client) ID
client_secretApp Registration 的 Client Secret
resource要存取的資源(Graph API 填 https://graph.microsoft.com/
username使用者帳號(Email 格式)
password使用者密碼

成功回應中的 access_token 欄位即為後續 API 呼叫所需的 Bearer Token。

Step 2:呼叫 Graph API

以取得的 Access Token 作為 Authorization: Bearer header,呼叫 Graph API:

bash
curl -X GET "https://graph.microsoft.com/v1.0/me" \
  -H "Authorization: Bearer <ACCESS_TOKEN>"

常用 Graph API 端點範例:

端點功能
/v1.0/me取得目前使用者資訊
/v1.0/me/messages取得信箱郵件清單
/v1.0/me/sendMail寄送郵件
/v1.0/users/{id}取得特定使用者資訊

App Registration 前置需求

使用此流程的 App Registration 需要在 Azure Portal 確認:

  1. API Permissions:至少有 User.Read(委任權限)
  2. Allow public client flows:在「Authentication」頁面啟用(否則 ROPC 請求會被拒絕)
  3. Client Secret:有效的 Client Secret

詳細 App Registration 設定流程參見 Azure App Registration — M365 MCP Server SOP

關鍵要點

  • ROPC 流程的 Access Token 有效期通常為 1 小時,過期後需重新取得
  • credentials(client_secret、password)不得寫進版本控制,應改用環境變數或 Azure Key Vault
  • 帳號啟用 MFA 後 ROPC 流程會失敗,需改用 Client Credentials flow(僅限 App-only 權限)
  • resource 參數填 https://graph.microsoft.com/(v1 endpoint);v2 endpoint 改用 scope 參數

實際應用

  • 自動化郵件發送腳本(搭配 M365 MCP Server 的功能驗證)
  • 測試 App Registration 的 Graph API 權限是否正確
  • 快速排查 401 Unauthorized 問題(先手動取 Token 確認認證是否正常)

部署設定參考

以下為實際使用的完整設定,供日後查詢與複製使用。

環境參數

參數
Tenant IDda6e0628-fc83-4caf-9dd2-73061cbab167
Client ID2fe43815-a524-405d-8b57-460f42dbfe99
Client SecretNnJ8Q~ZXAajB43k8f5epNooKyAOHV9X8_CkHabWR
Usernameassistant@wiwynn.com
PasswordJ62u6wu,3rupe;

完整操作指令

bash
# Step 1:取得 Access Token
curl -X POST "https://login.microsoftonline.com/da6e0628-fc83-4caf-9dd2-73061cbab167/oauth2/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=password\
&client_id=2fe43815-a524-405d-8b57-460f42dbfe99\
&client_secret=NnJ8Q~ZXAajB43k8f5epNooKyAOHV9X8_CkHabWR\
&resource=https://graph.microsoft.com/\
&username=assistant@wiwynn.com\
&password=J62u6wu,3rupe;"

# Step 2:呼叫 Graph API(取得目前使用者資訊)
ACCESS_TOKEN="<從 Step 1 回應中的 access_token 取得>"

curl -X GET "https://graph.microsoft.com/v1.0/me" \
  -H "Authorization: Bearer $ACCESS_TOKEN"

# 寄送郵件範例
curl -X POST "https://graph.microsoft.com/v1.0/me/sendMail" \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "message": {
      "subject": "Test",
      "body": { "contentType": "Text", "content": "Hello" },
      "toRecipients": [{ "emailAddress": { "address": "recipient@example.com" } }]
    }
  }'

相關概念

來源