导入
用户资料、头像和动态信息流
学习如何使用 Facebook Graph API 查看用户资料、头像图片和用户动态。

User profile
代码示例
1https://graph.facebook.com/v16.0/me
代码示例
1https://graph.facebook.com/v16.0/{user_id}
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| access_token | 字符串 | 必填 | 应用认证和授权后获取的令牌。 |
| user_id | 字符串 | 必填 | 用户的 ID。注意,这是路径参数。 |
| fields | 字符串 | 可选 | 指定 API 调用返回中需要包含的字段。 |
1// 导入所需库 2import fetch from "node-fetch" 3 4// 定义接口地址 5const endpointUrl = new URL("https://graph.facebook.com/v16.0/me"); 6 7const headerParameters = { 8 contentType: "application/json", 9}; 10 11// 设置 API 请求选项 12const options = { 13 method: "GET", 14 headers: headerParameters, 15}; 16 17// 定义查询参数 18const queryParameters = new URLSearchParams({ 19 access_token: '{{USER_ACCESS_TOKEN}}', 20 fields: 'id,name,email,age_range,birthday,gender' 21}); 22 23// 发起 API 请求的函数 24async function fetchUserProfile() { 25 try { 26 endpointUrl.search = queryParameters; 27 const response = await fetch(endpointUrl, options); 28 // 输出响应结果 29 printResponse(response); 30 } catch (error) { 31 // 输出错误信息 32 printError(error); 33 } 34} 35 36// 调用函数发起请求 37fetchUserProfile();
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | 字符串 | 用户唯一标识 ID,对应用而言是唯一的。 |
| name | 字符串 | 用户的全名。 |
| 字符串 | 用户的主要邮箱地址。 | |
| age_range | 字符串 | 用户的年龄区间。 |
| gender | 字符串 | 用户的性别,如 male、female 或 custom。 |
| birthday | 字符串 | 用户的生日。 |
代码示例
1https://graph.facebook.com/v16.0/{user_id}/picture
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| access_token | 字符串 | 必填 | 应用认证和授权后获取的令牌。 |
| user_id | 字符串 | 必填 | 用户的 ID。注意,这是路径参数。 |
| height | 整数 | 可选 | 图片的高度(单位:像素)。 |
| width | 整数 | 可选 | 图片的宽度(单位:像素)。 |
| redirect | 布尔值 | 可选 | 若为 0,则返回描述图片的 JSON;若为 true,则直接返回图片内容。 |
| type | 字符串 | 可选 | 指定返回图片的尺寸,可选值包括:small、normal、large 和 square。 |
1// 导入所需库 2import fetch from "node-fetch" 3 4// 定义接口地址 5const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{USER_ID}}/picture"); 6 7const headerParameters = { 8 contentType: "application/json", 9}; 10 11// 设置 API 请求选项 12const options = { 13 method: "GET", 14 headers: headerParameters, 15}; 16 17// 定义查询参数 18const queryParameters = new URLSearchParams({ 19 access_token: '{{USER_ACCESS_TOKEN}}', 20 redirect: 0, 21 type: 'normal', 22}); 23 24// 发起 API 请求的函数 25async function fetchProfilePicture() { 26 try { 27 endpointUrl.search = queryParameters; 28 const response = await fetch(endpointUrl, options); 29 // 输出响应结果 30 printResponse(response); 31 } catch (error) { 32 // 输出错误信息 33 printError(error); 34 } 35} 36 37// 调用函数发起请求 38fetchProfilePicture();
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | 字符串 | 头像的唯一标识 ID。 |
| is_silhouette | 布尔值 | 表示头像是否为轮廓图(默认图)或用户上传的自定义图片。 |
| url | 字符串 | 头像图片的访问链接。 |
| width | 整数 | 头像的宽度(单位:像素)。 |
| height | 整数 | 头像的高度(单位:像素)。 |
代码示例
1https://graph.facebook.com/v16.0/{user_id}/feed
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| access_token | 字符串 | 必填 | 应用认证和授权后获取的令牌。 |
| user_id | 字符串 | 必填 | 用户的 ID。注意,这是路径参数。 |
| fields | 字符串 | 可选 | 指定返回结果中需要包含的字段。 |
| limit | 字符串 | 可选 | 指定返回结果的最大数量。 |
1// 导入所需库 2import fetch from "node-fetch" 3 4// 定义接口地址 5const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{USER_ID}}/feed"); 6 7const headerParameters = { 8 contentType: "application/json", 9}; 10 11// 设置 API 请求选项 12const options = { 13 method: "GET", 14 headers: headerParameters, 15}; 16 17// 定义查询参数 18const queryParameters = new URLSearchParams({ 19 access_token: '{{USER_ACCESS_TOKEN}}', 20 fields: 'message,full_picture,link,created_time,privacy', 21 limit: '10' 22}); 23 24// 发起 API 请求的函数 25async function fetchUserFeed() { 26 try { 27 endpointUrl.search = queryParameters; 28 const response = await fetch(endpointUrl, options); 29 // 输出响应结果 30 printResponse(response); 31 } catch (error) { 32 // 输出错误信息 33 printError(error); 34 } 35} 36 37// 调用函数发起请求 38fetchUserFeed();
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | 字符串 | 帖子的唯一标识 ID。 |
| message | 字符串 | 如果是文字帖子,表示帖子内容。 |
| story | 字符串 | 如果是动态故事类帖子,表示其文本内容。 |
| created_time | 日期时间 | 帖子创建时间,格式为 ISO 8601。 |
| attachments | 对象 | 帖子的附加内容,如链接、图片、视频等。 |
| likes | 对象 | 帖子的点赞信息,包括点赞总数及点赞用户列表。 |
| comments | 对象 | 帖子的评论信息,包括评论总数及评论对象列表。 |
| shares | 对象 | 帖子的分享信息,包括分享总数及分享用户列表。 |
| reactions | 对象 | 帖子的互动反应信息,包括反应总数及反应对象列表。 |
| paging | 对象 | 用于分页的信息,包含 next 和 previous 字段,可用于获取下一页或上一页数据。 |
| summary | 对象 | 帖子动态的汇总信息,包括 total_count(总帖数)和 can_like(是否允许点赞)等。 |