【发布时间】: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