BuddyPress Rest Api 使用小记

BuddyPress REST API 允许您从第三方站点或应用程序与 BuddyPress 交互。

您可以执行诸如抓取活动源、上传活动更新、与群组和成员互动等操作。它与插件中的 PHP 功能没有 100% 相同,但它拥有大部分好东西。

BP REST API 应该在 5.0 版中添加到 BuddyPress 核心插件中,但目前它在 Github 上可用。它仍然是 beta-ish,但我们已经成功使用应用程序的修改版本几个月了。

在本文中,我将向您介绍 BP REST API 的主要组件,以及我们使用它们的一些技巧。

建议下载一个 Postman 方便调试使用

Getting Started

安装BP插件:

确保您至少使用 WP 4.7 和 BuddyPress 4.0 以上,然后从 Github 添加 BP REST 插件。您可以下载主 .zip 文件并在您的插件目录中解压缩,然后激活。

要测试是否正确安装,请登录您的站点。然后使用 Postman 向 https://site.com/wp-json/buddypress/v1/activity 发送 GET 请求

您应该会看到活动数据列表。

BuddyPress Rest Api 使用小记

Authentication

如果您从 WordPress 插件或主题调用 API,则无需担心身份验证。 API 将检测用户是否登录,并返回适当的数据。

如果您使用的是 3rd 方应用程序,则不能使用 cookie,因此您需要使用 OAuth JWT。 JWT (json web token) auth 很好用,而且比 oAuth 更简单,所以我们将使用它。

设置 JWT Auth

安装此插件,并按照配置说明进行操作。

确保您定义了您的密钥,并通过将此代码添加到您的 wp-config.php 文件来启用 CORS。

方法:

  1. 进入 WordPress 根目录,使用代码编辑器(比如notepad++或者vscode等,或者宝塔里面直接编辑)打开wp-config.php文件。
  2. 在 $table_prefix = 'wp_'; 下面一行加上如下代码:
define('JWT_AUTH_SECRET_KEY', '32位字符串');
define('JWT_AUTH_CORS_ENABLE', true);

32位字符从下面网址获取:

打开网址:https://api.wordpress.org/secret-key/1.1/salt/ 然后参考下图复制32位字符串

BuddyPress Rest Api 使用小记

如果您的环境使用的是 Apache ,请将下面的代码复制到 .htaccess 文件中。(如果是宝塔,请直接在站点设置,伪静态设置里面操作)

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

然后,您应该能够通过使用您的用户名和密码作为 url 参数向 /wp-json/jwt-auth/v1/token 发送 POST 请求来获取令牌。保存该令牌,我们会将其硬编码到我们的 API 调用中。 (对于一个成熟的应用程序,您需要为此创建一个 UI,但这是另一个教程)

BuddyPress Rest Api 使用小记
Authentication token for JWT with the WordPress REST API

正常的话就是跳转到这页面,不过也看主题。比如我的主题和部分插件,导致我的密码输入错误的情况下没有返回json响应,而是跳转到了登录页面。

现在可以在授权标头中使用此 Authorization header 来获取敏感数据,例如私人消息。

这个头信息是Authorization: Bearer空格+获取的token

BuddyPress Rest Api 使用小记

其他请求示例不再过多说明,官方文档都给的比较清楚了 https://developer.buddypress.org/bp-rest-api/

给出几个例子:

Activity

要获取所有站点活动,请向 http://site.com/wp-json/buddypress/v1/activity 发送 GET 请求

以下是撰写本文时可用的参数列表:

NameTypeDescription
contextstringScope under which the request is made; determines fields present in response.
Default: view
One of: viewedit
pageintegerCurrent page of the collection of activities.
Default: 1
per_pageintegerMaximum number of activities to be returned in result set.
Default: 10
searchstringLimit results to those matching a string.
excludearrayEnsure result set excludes specific IDs.
Default: []
includearrayEnsure result set includes specific IDs.
Default: []
orderstringOrder sort attribute ascending or descending.
Default: desc
One of: asc, desc
afterstring,
date-time
Limit result set to activities published after a given ISO8601 compliant date.
user_idintegerLimit result set to activities created by a specific member (ID).
statusstringLimit result set to items with a specific status.
Default: ham_only
One of: ham_only, spam_only, all
scopestringLimit result set to items with a specific scope.
Default: []
One of : just-mefriendsgroupsfavorites, mentions
group_idintegerLimit result set to activities created within a specific BuddyPress Group.
site_idintegerLimit result set to activities attached to a specific site.
primary_idintegerLimit result set to activities with a specific primary association ID.
secondary_idintegerLimit result set to activities with a specific secondary association ID.
componentstringLimit result set to items with a specific active BuddyPress component.
One of: the active BuddyPress components
typearrayLimit result set to activities with one or more specific activity type.
One or more of: the registered activity types
display_commentsstringNo comments by default, stream for within stream display, threaded for below each activity item.
Default: ''

使用参数的示例如下:

http://site.com/wp-json/buddypress/v1/activity?group_id=2&display_comments=false&per_page=12

要发布新活动,请向同一端点发出 POST 请求。唯一需要的参数是“内容”,当然您必须如上所述在授权标头中发送您的 token。

BuddyPress Rest Api 使用小记

要发布活动评论,您可以发出相同的请求,但添加一个 activity_comment 类型并包括父项和 id。

https://site.com/wp-json/buddypress/v1/activity?content=mycomment&parent=247&id=247&type=activity_comment

给TA打赏
共{{data.count}}人
人已打赏
后端/架构程序代码

使用 Pillow Python 库处理图像

2022-2-9 10:42:11

程序代码

又一款Typecho小程序--RuleApp初体验

2022-3-6 16:21:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索