Logo
Section 1 / 2

用户资料、头像和动态信息流

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

导入

用户资料、头像和动态信息流

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

User profile
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字符串用户的全名。
email字符串用户的主要邮箱地址。
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(是否允许点赞)等。

简介

点赞、用户帖子、照片和视频