从POST请求接收JSON响应

使用POST请求,我的目标是从JSON响应中接收授权码。但是,我得到的唯一响应是我连接到的网页的HTML,而不是所需的JSON响应。

<input type="checkbox" id="c">
w459537795 回答:从POST请求接收JSON响应

我认为supplied API documentation for Skubana的指定令人震惊地不足。

您可能会错过的部分是

  
      
  1. 添加请求路径:/ oauth / token
  2.   

公平地说,它们不会反映在其“示例网址”中:

  

示例网址:https://blah.skubana.com?grant_type=authorization_code&redirect_uri=redirect.com/callback&code=ABCDE&cid=ABCD1234

当您更改URL以包括该路径时,我会收到401未经授权的回复:

>>> url = "http://demo.skubana.com/oauth/token?grant_type=authorization_code&redirect_uri=demo.skubana.com/appstore&code=LCqYHU&cid=Y29tcGFueUlkMTI0MDYw"
>>> r = requests.post(url,headers=headers)
>>> r.status_code
401

顺便说一句,这也涉及从POST到GET的重定向。但是我强烈怀疑具有正确凭据的 (我没有),您实际上会获得令牌。

发布到https://demo.skubana.com时,您将被重定向,首先使用相同的URL进行GET请求,

Location: https://demo.skubana.com/?grant_type=authorization_code&redirect_uri=demo.skubana.com/appstore&code=LCqYHU&cid=Y29tcGFueUlkMTI0MDYw

然后转到您的仪表板:

Location: /work/dashboard

最后进入登录页面:

Location: https://demo.skubana.com/login

所有这些都记录在r.history列表中:

>>> r.history
[<Response [302]>,<Response [302]>,<Response [302]>]

请注意,您不必设置json={} ,只需使用:

r = requests.post(url,headers=headers)

请求库可以为您处理Base64编码,因为它仅为part of the Basic Authentication scheme,只需将YOUR_APP_KEY和YOUR_APP_SECRET值作为用户名和密码传递:

YOUR_APP_KEY = "QT8txxxxxxxx"
YOUR_APP_SECRET = "n76mxxxxxxxxx"

url = "http://demo.skubana.com/oauth/token"
parameters = {
    "grant_type": "authorization_code","redirect_uri": "demo.skubana.com/appstore","code": "LCqYHU","cid": "Y29tcGFueUlkMTI0MDYw",}

r = requests.post(url,params=parameters,auth=(YOUR_APP_KEY,YOUR_APP_SECRET))

最后,鉴于重定向和完全缺少POST参数,我强烈怀疑GET请求的效果也一样:

r = requests.get(url,YOUR_APP_SECRET))

这只是绕过POST-> GET重定向。

通常,像这样的OAuth2请求令牌会在POST正文中发布参数 ,所以也可以尝试:

r = requests.post(url,data=parameters,YOUR_APP_SECRET))

如果params=parameters选项仍然无法获得您的令牌,

本文链接:https://www.f2er.com/3165407.html

大家都在问