知晓云开发api 如何用python进行oauth2 认证

希望用python requests操作知晓云的开放api,但是总是失败,求大神解惑。

这是他们的js示例:

https://doc.minapp.com/open-a...

  var request = require('request');

  // 获取 code
  var opt = {
    uri: 'https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/',
    method: 'POST',
    json: {
      client_id: 'a4d2d62965ddb57fa4xx',
      client_secret: 'e5802b40135baab9b4e84e35bed058a264c37dxx'
    },
    jar: true,                // 允许记住 cookie 
    followAllRedirects: true,     // 允许重定向
  }

  request(opt, function(err, res, body) {
      getToken(body.code)  // 回调调用 getToken 函数
  })

  // 获取 token
  function getToken(code) {
    var opt = {
      uri: 'https://cloud.minapp.com/api/oauth2/access_token/',
      method: 'POST',
      formData: {   // 指定 data 以 "Content-Type": "multipart/form-data" 传送
        client_id: 'a4d2d62965ddb57fa4xx',
        client_secret: 'e5802b40135baab9b4e84e35bed058a264c37dxx',
        grant_type: 'authorization_code',
        code,
      }
    }

    request(opt, function(err, res, body) {
      let token = JSON.parse(body).access_token
    })
  }

这是我的python代码:

import requests
import json

client_id = '谢谢谢谢'
client_secret = '非常感谢'
r = requests.post('https://cloud.minapp.com/api/oauth2/hydrogen/openapi/authorize/', json={"client_id": client_id, "client_secret": client_secret})
code = json.loads(r.content)["code"]
print code
#code可以正常获取

headers = {'Content-Type': 'multipart/form-data'}
payload = {"client_id":client_id,'client_secret':client_secret, 'grant_type' : 'authorization_code', 'code' : code}
r = requests.post("https://cloud.minapp.com/api/oauth2/access_token/", headers=headers, data=payload)
print r.content

不知道为啥总是返回400

阅读 5.2k
2 个回答

400是客户端错误,bad request,估计和cookie有关。

新手上路,请多包涵

您好,您这个问题解决了嘛?我最近也想弄弄知晓云这个open api 没想到第一步就卡住了。。。。。求方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题