C#应用程序集成WordPressPCL管理WordPress REST-API网站

.Net技术 码拜 4年前 (2020-01-27) 3414次浏览 0个评论

WordPressPCL

这是一个很简单易用的类库,用于在C# 应用程序中管理 WordPress REST-API,如果你发现了bug或者有任何建议,都可以在github上提问。

License

WordPressPCL 的发布基于 MIT License

快速入门

WordPress 前提条件

Since WordPress 4.7 the REST API has been integrated into the core so there’s no need for any plugins to get basic functionality. If you want to access protected endpoints, this library supports authentication through JSON Web Tokens (JWT) (plugin required).

自从WordPress 4.7 开始 REST API 已经集成到核心中,所以并不需要额外的插件就可以调用基本的函数。如果你需要访问一些受保护的功能,这个类库支持JWT(JSON Web Tokens)授权(这需要在wordpress后台安装额外的插件)。

  • WordPress 4.7 及以上版本
  • 安装插件:JWT Authentication for WP REST API

集成 WordPressPCL

可以通过 NuGet 在项目中安装 WordPressPCL (在程序包管理器控制台执行命令):

> Install-Package WordPressPCL

支持的平台

WordPressPCL基于netstandard  1.1 and 2.0 ,所以它应该支持在以下平台:

  • .NET Framework 4.5 及更高版本
  • .NET Core
  • Universal Windows Platform (uap)
  • Windows 8.0 及更高版本
  • Windows Phone (WinRT, 非 Silverlight)
  • Mono / Xamarin

快速开始向导:

// Initialize
var client = new WordPressClient("http://demo.wp-api.org/wp-json/");

// Posts
var posts = await client.Posts.GetAll();
var postbyid = await client.Posts.GetById(id);

// Comments
var comments = await client.Comments.GetAll();
var commentbyid = await client.Comments.GetById(id);
var commentsbypost = await client.Comments.GetCommentsForPost(postid, true, false);

// Users
// JWT authentication
var client = new WordPressClient(ApiCredentials.WordPressUri);
client.AuthMethod = AuthMethod.JWT;
await client.RequestJWToken(ApiCredentials.Username,ApiCredentials.Password);

// check if authentication has been successful
var isValidToken = await client.IsValidJWToken();

// now you can send requests that require authentication
var response = client.Posts.Delete(postid);

支持的 REST 方法

Create Read Update Delete
Posts yes yes yes yes
Pages yes yes yes yes
Comments yes yes yes yes
Categories yes yes yes yes
Tags yes yes yes yes
Users yes yes yes yes
Media yes yes yes yes
Post Revisions yes yes
Taxonomies yes
Post Types yes
Post Statuses yes
Settings yes yes

其他特性

  • 授权使用的 JSON Web Tokens (JWT)
  • HttpResponsePreProcessing: 在反序列化API响应之前对其进行操作

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明C#应用程序集成WordPressPCL管理WordPress REST-API网站
喜欢 (1)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!