导入
创建、更新和删除帖子
了解博客文章的相关知识,以及如何创建、更新和删除博客文章。
| 参数 | 类型 | 描述 |
|---|---|---|
| kind | string | 对象的类型。值:blogger#post。此值固定不变。 |
| id | string | 文章的ID。 |
| blog | object | 包含文章所属博客数据的对象。 |
| blog.id | string | 文章所属博客的ID。 |
| published | datetime | 文章的发布日期。格式:yyyy-mm-dd |
| updated | datetime | 文章最后一次更新的日期。格式:yyyy-mm-dd |
| url | string | 文章的发布链接。 |
| selfLink | string | 文章的Blogger API URL。 |
| title | string | 文章的标题。 |
| content | string | 文章的内容。注意:该值可能包含HTML。 |
| author | object | 包含文章作者信息的对象。 |
| author.id | string | 文章作者的ID。 |
| author.displayName | string | 文章作者的名称。 |
| author.url | string | 作者个人资料的发布链接。 |
| author.image | object | 包含作者头像信息的对象。 |
| author.image.url | string | 作者头像的URL。 |
| replies | object | 包含文章评论信息的对象。 |
| replies.totalItems | long | 文章的评论总数。 |
| replies.selfLink | string | 文章评论的Blogger API URL。 |
| labels[] | list | 与文章关联的标签列表。 |
| customMetaData | string | 文章的元数据。 |
| replies.items[] | list | 文章的评论列表。 |
| location | object | 包含文章地理位置信息的对象。注意:仅当文章被标记了地理坐标时才可用。 |
| location.name | string | 文章所在位置的名称。 |
| location.lat | double | 文章位置的纬度。 |
| location.lng | double | 文章位置的经度。 |
| location.span | string | 文章位置的视图范围。 |
| titleLink | string | 标题的URL。 |
| images[] | list | 文章的展示图片。 |
| images[].url | string | 文章展示图片的URL。 |
| status | string | 文章的状态。注意:此值仅对管理员级别请求有效。 |
1https://blogger.googleapis.com/v3/blogs/{blogId}/posts
请求参数
| 参数 | 类型 | 类别 | 描述 |
|---|---|---|---|
| blogId | string | 必需 | 要添加文章的博客ID。 |
| isDraft | boolean | 可选 | 决定是否将文章创建为草稿。允许值:true 表示创建为草稿,false 表示不创建为草稿。 |
1// 在此处定义导入库 2import fetch from 'node-fetch'; 3 4// 在此处定义访问令牌 5const ACCESS_TOKEN = '{{ACCESS_TOKEN}}'; 6 7// 在此处定义路径参数 8const blogId = '{{BLOG_ID}}'; 9 10// 在此处定义端点URL 11const endpointUrl = new URL(`https://blogger.googleapis.com/v3/blogs/${blogId}/posts`); 12 13// 在此处定义头参数 14const headerParameters = { 15 authorization: `Bearer ${ACCESS_TOKEN}`, 16 contentType: 'application/json', 17}; 18 19// 在此处定义查询参数 20const queryParameter = new URLSearchParams({ 21}); 22 23// 在此处定义请求体参数 24const bodyParameters = JSON.stringify({ 25 title: '', 26 content: '', 27}); 28 29// 设置API调用选项 30const options = { 31 method: 'POST', 32 headers: headerParameters, 33 body: bodyParameters, 34}; 35 36// 定义API调用函数 37async function createPost() { 38 try { 39 endpointUrl.search = queryParameter; 40 const response = await fetch(endpointUrl, options); 41 42 // 输出响应 43 printResponse(response); 44 } catch (error) { 45 // 输出错误信息 46 printError(error); 47 } 48} 49 50// 调用函数执行API调用 51createPost();
1https://blogger.googleapis.com/v3/blogs/{blogId}/posts/{postId}
请求参数
| 参数 | 类型 | 类别 | 描述 |
|---|---|---|---|
| blogId | string | 必需 | 要更新文章的博客ID。 |
| postId | string | 必需 | 要更新的文章ID。 |
1// 在此处导入库 2import fetch from "node-fetch"; 3 4// 在此处定义访问令牌 5const ACCESS_TOKEN = '{{ACCESS_TOKEN}}'; 6 7// 在此处定义路径参数 8const blogId = '{{BLOG_ID}}'; 9const postId = '{{POST_ID}}'; 10 11// 在此处定义端点URL 12const endpointUrl = new URL(`https://blogger.googleapis.com/v3/blogs/${blogId}/posts/${postId}`); 13 14// 在此处定义头参数 15const headerParameters = { 16 authorization: `Bearer ${ACCESS_TOKEN}`, 17 contentType: 'application/json', 18}; 19 20// 在此处定义请求体参数 21const bodyParameters = JSON.stringify({ 22 title: '', 23}); 24 25// 设置API调用选项 26const options = { 27 method: 'PUT', 28 headers: headerParameters, 29 body: bodyParameters, 30}; 31 32// 定义API调用函数 33async function updatePost() { 34 try { 35 const response = await fetch(endpointUrl, options); 36 37 // 输出响应 38 printResponse(response); 39 } catch (error) { 40 // 输出错误信息 41 printError(error); 42 } 43} 44 45// 调用函数执行API调用 46updatePost();
1https://blogger.googleapis.com/v3/blogs/{blogId}/posts/{postId}
请求参数
| 参数 | 类型 | 类别 | 描述 |
|---|---|---|---|
| blogId | string | 必需 | 要删除文章的博客ID。 |
| postId | string | 必需 | 要删除的文章ID。 |
1// 在此处定义访问令牌 2const ACCESS_TOKEN = '{{ACCESS_TOKEN}}'; 3 4// 在此处定义路径参数 5const blogId = '{{BLOG_ID}}'; 6 7// 在此处定义端点URL 8const endpointUrl = new URL('https://blogger.googleapis.com'); 9 10// 在此处定义头参数 11const headerParameters = { 12 authorization: `Bearer ${ACCESS_TOKEN}`, 13 contentType: 'application/json', 14}; 15 16// 设置API调用选项 17const options = { 18 method: 'DELETE', 19 headers: headerParameters, 20}; 21 22// 定义API调用函数 23async function deletePost() { 24 try { 25 // 创建一篇文章并获取其ID 26 const postId = await createPostAndGetId(); 27 if (!postId) 28 return; 29 endpointUrl.pathname = `/v3/blogs/${blogId}/posts/${postId}`; 30 const response = await fetch(endpointUrl, options); 31 32 // 输出响应 33 printResponse(response); 34 } catch (error) { 35 // 输出错误信息 36 printError(error); 37 } 38} 39 40// 调用函数执行API调用 41deletePost();