【问题标题】:How to get, delete or publish wordpress 4.9 post from powershell using Rest API?如何使用 Rest API 从 powershell 获取、删除或发布 wordpress 4.9 帖子?
【发布时间】:2018-07-04 07:12:31
【问题描述】:

我在 technet 上找到了这个powershell script。描述说:第 1 步 - 由 WP REST API 团队安装“JSON 基本身份验证”和“WP REST API”。 虽然 WP REST API 团队说“The REST API is included in WordPress 4.7”,所以不再需要插件。

但现在 wp-restApi.ps1 脚本不再适用于 WP 4.9。返回下一个错误“Invoke-WebRequest:远程服务器返回错误:(401) Unauthorized。”

如何使用 powershell Invoke-WebRequest -method post 发布到 WP 4.7 及更高版本?

#post title and content 
$params = @{ 
    title = "test Rest API post" 
    content = "test Rest API post content" 
    status = 'publish' 
} 
#change username and password before use 
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("user:pass@23"))) 
$header = @{ 
Authorization=("Basic {0}" -f $base64AuthInfo) 
} 
$params1=$params|ConvertTo-Json 
Invoke-RestMethod -Method post -Uri http://khaoodara.com/wp-json/wp/v2/posts -ContentType "application/json" -Body $params1  -Headers $header -UseBasicParsing 

【问题讨论】:

  • 您尝试进行更改的帐户无权执行您正在尝试的操作,因此 HTTP401..
  • @TheIncorrigible1 该帐户具有版主级别权限

标签: json wordpress powershell http-post


【解决方案1】:

原来你需要安装 WP-API/Basic-Auth Wordpress plugin 并且脚本开始工作。

【讨论】:

    猜你喜欢
    • 2018-04-05
    • 1970-01-01
    • 2021-04-06
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多