Facebook Graph API 是 Facebook 提供的用于与 Facebook 平台交互的主要方式,它基于 RESTful 架构,允许开发者读取和写入 Facebook 社交图谱中的数据。
Graph API 默认返回 JSON 格式的数据,主要包含以下结构:
{
"data": [
{
"id": "123456789",
"name": "John Doe",
"email": "john.doe@example.com"
}
],
"paging": {
"cursors": {
"before": "QVFIU...",
"after": "QVFIU..."
},
"next": "https://graph.facebook.com/v12.0/me/friends?access_token=..."
}
}
{
"error": {
"message": "Invalid OAuth access token.",
"type": "OAuthException",
"code": 190,
"error_subcode": 463,
"fbtrace_id": "AbCdEfGhIjKlMnOpQrStUvWxYz"
}
}
// 使用 fetch API 获取数据
fetch('https://graph.facebook.com/v12.0/me?fields=id,name,email&access_token=YOUR_TOKEN')
.then(response => response.json())
.then(data => {
if (data.error) {
console.error('Error:', data.error.message);
} else {
console.log('User ID:', data.id);
console.log('Name:', data.name);
console.log('Email:', data.email);
}
})
.catch(error => console.error('Request failed:', error));
import requests
import json
response = requests.get(
'https://graph.facebook.com/v12.0/me',
params={
'fields': 'id,name,email',
'access_token': 'YOUR_TOKEN'
}
)
data = response.json()
if 'error' in data:
print(f"Error: {data['error']['message']}")
else:
print(f"User ID: {data['id']}")
print(f"Name: {data['name']}")
print(f"Email: {data.get('email', 'Not available')}")