需要获得所需的 http 帖子 [英] Need to get required http post

查看:30
本文介绍了需要获得所需的 http 帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 Huddle Api 说明获取访问令牌.我正在使用powershell发布如下方法:

I am following Huddle Api instructions to get the Access Token. I am using powershell to post the method which is as follows:

POST /token HTTP/1.1
Host: login.huddle.net
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&client_id=s6BhdRkqt&redirect_uri=MyAppServer.com/receiveAuthCode&code=i1WsRn1uB1

我使用的 Powershell 命令是:

Powershell Command which I am using is:

$body = { '@grant_type' = 'authorization_code'; client_id = 'xxxxx';
           redirect_uri = 'myAppServer.com'; code = '123abcdef' } 

Invoke-WebRequest -Uri "login.huddle.com" -ContentType "application/x-www-form-urlencoded" -Method Post

这有效,我得到了200 OK"的响应,还显示了访问令牌的激活.我将如何检索访问令牌编号.例如,我需要他们在指令中提到的输出:

This works and I get the response of "200 OK" and also shows the activation of Access Token. How would I retrieve the Access Token number. For example, I need the output as they mentioned in instruction which is:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store

{
 "access_token":"S1AV32hkKG",
 "expires_in":300,
 "refresh_token":"8xLOxBtZp8"
}

我认为它与 ContentType 有关系.所以我确实尝试过应用程序/Json",但事实并非如此.有什么建议?

I think it has something to do ContentType. So I did try, "application/Json" but that was not it. Any suggestions?

推荐答案

您使用了错误的 cmdlet.由于您提到获取 StatusCode、Content、RawContent 等的值,这告诉我们您正在使用 Invoke-WebRequest.这个 cmdlet 很棒……但不适用于 API,API 通常是 REST 格式并使用 JSON.IWR 可以处理请求,但您必须深入研究 $Response.Content 并从 JSON 转换.

You're using the wrong cmdlet. Since you mentiond getting back values for StatusCode, Content, RawContent, etc, that tells us that you're using Invoke-WebRequest. This cmdlets awesome...but not for working with APIs, which are commonly REST formatted and use JSON. IWR can handle the request but you have to dig into the $Response.Content and convert from JSON.

尝试使用 Invoke-RestMethod 代替 Invoke-WebRequest.您很可能会返回 AccessCode,但它是作为 JSON 格式的属性返回的.Invoke-RestMethod 将原生解析 JSON 并将其转换为 PowerShell 对象.您可以将其替换为 Invoke-WebRequest,它应该正常工作.

Instead of Invoke-WebRequest, try using Invoke-RestMethod. It's likely that you are getting the AccessCode returned, but as a JSON formatted property. Invoke-RestMethod will natively parse and convert JSON into PowerShell objects. You can just sub it in for Invoke-WebRequest and it should just work.

Invoke-RestMethod -Uri "login.huddle.com" -ContentType "application/x-www-form-urlencoded" -Method Post -body $body

这篇关于需要获得所需的 http 帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆