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

查看:180
本文介绍了需要获得必要的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的响应,并显示Access Token的激活。我如何检索访问令牌号码。例如,我需要在指令中提到的输出:

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 。所以我尝试了application / 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。这个cmdts很棒...但不适合使用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天全站免登陆