Invoke-RestMethod:远程服务器返回错误:(400)错误请求

时间:2015-04-14 09:34:27

标签: rest powershell

我正在尝试使用Wordpress在我的网站上编辑帖子,但我不知道为什么它不起作用。

接收访问令牌:确定

验证令牌:确定

Invoke-WebRequest -Uri "https://public-api.wordpress.com/oauth2/token-info?client_id=$clientID&token=$token"

没关系,我得到以下内容:

{
    "client_id": "your client ID",
    "user_id": "user ID",
    "blog_id": "blog ID",
    "scope": "scope of the token"
}

验证没问题,我的令牌有效,我不认为它的身份验证相关。

但是当我运行这段代码时,我总是得到:

Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:1 char:1
+ Invoke-RestMethod -Method Post -Uri "https://public-api.wordpress.com/rest/v1.1/ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand



$body = @{Content="This is a test";Title="Test"} | ConvertTo-Json
$token = 'abcdefs21ds1ds1ds1dsd2s1d2'

$headers = @{
    "Authorization" = "Bearer $token"
}

Invoke-RestMethod -Method Post -Uri "https://public-api.wordpress.com/rest/v1.1/sites/www.domain.com/posts/1234" -Headers $headers -Body $body -ContentType 'application/json'

文档:https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/%24post_ID/

0 个答案:

没有答案