คู่มือเต็มรูปแบบสำหรับโปรเจกต์ lnwAdsSJ88 — ตั้งแต่สมัคร Developer จนถึงดึงข้อมูล Ads ได้จริง
ก่อนจะใช้ Facebook API ได้ ต้องลงทะเบียนเป็น Meta Developer ก่อน
เมื่อสมัครเสร็จ จะเห็นหน้า My Apps ที่ developers.facebook.com/apps/
แอปคือตัวกลางที่ให้คุณเข้าถึง API ต้องสร้าง 1 แอปต่อ 1 โปรเจกต์
lnwAds Dashboard) และ Contact
EmailMeasure ad performance data with Marketing APICreate & manage ads with Marketing API
เชื่อมแอปกับ Business Portfolio เพื่อให้ Token เข้าถึง Ad Account ได้
Ad Account ID ต้องเป็นรูปแบบ act_<digits> เช่น act_2807991909289645
ดูได้ที่ Business Settings > Accounts > Ad Accounts
ต้องขอสิทธิ์จาก Meta เพื่อเข้าถึงข้อมูลโฆษณา
| Permission | ใช้เมื่อ | ความจำเป็น |
|---|---|---|
ads_read |
อ่านรายงาน Insights, ดูแคมเปญ | ✅ จำเป็น (ขั้นต่ำ) |
ads_management |
สร้าง/แก้ไข/หยุดแคมเปญ | ⚡ ถ้าต้องจัดการ |
business_management |
จัดการ Business Asset | 🔧 เฉพาะกรณีจำเป็น |
Ads Management Standard Access
ads_management และ
ads_read ที่จะใช้งาน
Access Token คือ "กุญแจ" ที่ใช้เข้าถึง API ทุกครั้ง
ads_read (และ ads_management ถ้าต้องใช้)
curl -G "https://graph.facebook.com/v24.0/oauth/access_token" \
-d "grant_type=fb_exchange_token" \
-d "client_id=<APP_ID>" \
-d "client_secret=<APP_SECRET>" \
-d "fb_exchange_token=<SHORT_LIVED_TOKEN>"
.env หรือ config file ที่อยู่ใน .gitignoreทดสอบว่า Token ใช้ได้จริงผ่าน Terminal
curl -G "https://graph.facebook.com/v24.0/me" \
-d "access_token=$META_ACCESS_TOKEN" \
-d "fields=id,name"
{"id":"...", "name":"ชื่อบัญชี"}curl -G "https://graph.facebook.com/v24.0/me/adaccounts" \
-d "access_token=$META_ACCESS_TOKEN" \
-d "fields=id,account_id,name,account_status,currency"
curl -G "https://graph.facebook.com/v24.0/act_2807991909289645/insights" \
-d "access_token=$META_ACCESS_TOKEN" \
-d "date_preset=last_7d" \
-d "level=campaign" \
-d "fields=campaign_id,campaign_name,spend,impressions,reach,clicks,ctr,cpc"
curl -G "https://graph.facebook.com/v24.0/act_2807991909289645/insights" \
-d "access_token=$META_ACCESS_TOKEN" \
-d "level=campaign" \
-d 'time_range={"since":"2026-01-01","until":"2026-01-31"}' \
-d "fields=campaign_id,campaign_name,spend,impressions,reach,clicks"
import json, os, requests
TOKEN = os.environ["META_ACCESS_TOKEN"]
AD_ACCOUNT = "act_2807991909289645"
URL = f"https://graph.facebook.com/v24.0/{AD_ACCOUNT}/insights"
params = {
"access_token": TOKEN,
"level": "campaign",
"time_range": '{"since":"2026-01-01","until":"2026-01-31"}',
"fields": "campaign_id,campaign_name,spend,impressions,reach,clicks,ctr,cpc",
}
resp = requests.get(URL, params=params, timeout=60)
resp.raise_for_status()
data = resp.json()
os.makedirs("data/raw", exist_ok=True)
with open("data/raw/insights_jan.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)
print(f"Saved {len(data.get('data',[]))} rows")
const token = process.env.META_ACCESS_TOKEN;
const adAccount = "act_2807991909289645";
const params = new URLSearchParams({
access_token: token,
level: "campaign",
date_preset: "last_7d",
fields: "campaign_id,campaign_name,spend,impressions,clicks",
});
const url = `https://graph.facebook.com/v24.0/${adAccount}/insights?${params}`;
const res = await fetch(url);
const json = await res.json();
console.log(json.data);
| อาการ | สาเหตุ | วิธีแก้ |
|---|---|---|
{"data":[]} |
ช่วงเวลาที่เลือกไม่มี spend | ลองเปลี่ยนช่วงวันที่ให้กว้างขึ้น |
| Error 190 | Token หมดอายุ / ถูก revoke | สร้าง Token ใหม่ที่ Graph Explorer |
| Error 200 (permissions) | ยังไม่ได้ขอสิทธิ์ ads_read |
ไปขอ Permission ที่ App Review |
| Error 100 (invalid parameter) | ชื่อ field ผิด หรือ API version ไม่รองรับ | เช็ก docs ว่า field ยังใช้ได้กับ v24.0 |
| Rate limit / timeout | Query หนักเกินไป | แบ่งช่วงเวลาให้สั้นลง, ลดจำนวน fields |
| Error 17 (too many calls) | โดน Rate Limit | รอ 5-10 นาที แล้วลองใหม่, ใช้ exponential backoff |
โปรเจกต์ lnwAdsSJ88 มีระบบ API พร้อมใช้อยู่แล้วใน AppApi/
แก้ไฟล์ AppApi/config/meta_integration.json:
{
"access_token": "<TOKEN ของคุณ>",
"ad_account_id": "act_2807991909289645",
"api_version": "v24.0",
"timezone": "Asia/Bangkok",
"app_id": "<APP_ID>",
"app_secret": "<APP_SECRET>"
}
# รัน local server
python3 AppApi/server.py --port 8787
# เปิดเบราว์เซอร์
# http://127.0.0.1:8787/settings.html → กรอก Token + ทดสอบ
# http://127.0.0.1:8787/index.html → คู่มือ API แบบเต็ม
# ดู Ad Accounts
python3 AppApi/scripts/fetch_adaccounts.py \
--config AppApi/config/meta_integration.json --json
# ดึง Insights
python3 AppApi/scripts/fetch_insights.py \
--config AppApi/config/meta_integration.json \
--since 2026-01-01 --until 2026-01-31 --json
# Sync เข้า Database
python3 AppApi/scripts/sync_insights_to_db.py \
--config AppApi/config/meta_integration.json \
--db data/facebook/ads.db \
--since 2026-01-01 --until 2026-01-31 --json
| ตรวจสอบ | ผลที่ต้องได้ |
|---|---|
/api/integration/test |
ok: true |
/api/integration/adaccounts |
แสดงรายการ account ≥ 1 |
/api/integration/sync |
rows_total > 0 |
ตาราง import_logs |
มี record ที่ import_type = meta_api |
| กรณีใช้งาน | สิทธิ์ขั้นต่ำ | ทำอะไรได้ | ทำไม่ได้ |
|---|---|---|---|
| ดูรายงานอย่างเดียว | ads_read |
อ่าน account/campaign/insights ได้ครบสำหรับ dashboard | เพิ่มงบ/ลดงบ/pause campaign ไม่ได้ |
| ต้องการปรับงบอัตโนมัติ | ads_management |
เขียนค่า campaign/adset เช่น budget, status ได้ | ใช้งานไม่ได้ถ้ายังไม่ผ่าน review หรือไม่มี role ที่ถูกต้อง |
ads_read ก่อนเสมอ แล้วค่อยขยายเป็น ads_management
เมื่อ flow รายงานนิ่งและมี audit log พร้อม
api.json (ใช้กับหน้า Live Dashboard){
"access_token": "<TOKEN>",
"ad_account_id": "act_2807991909289645",
"api_version": "v24.0",
"timezone": "Asia/Bangkok",
"use_case": "ads_read"
}
/demo/live-dashboard แล้วเลือกบัญชีโฆษณา| รายการ | เกณฑ์ผ่าน |
|---|---|
| การเก็บ Token | เก็บใน env/secret manager เท่านั้น, ไม่โชว์ค่าเต็มบนหน้าเว็บ |
| การเข้าถึง API | มี rate limit + retry + timeout และบันทึก error reason |
| ฐานข้อมูล | มี cache TTL + checkpoint + backup ก่อน deploy |
| การติดตามย้อนหลัง | มี log ว่าใครสั่งอะไร เมื่อไหร่ และผลเป็นอย่างไร |