从 python 到 HTTR

From python to HTTR

我有以下参数可以通过 python

在 api 上验证我的身份
library(reticulate)
import requests
url = 'https://api.checkbox.com/v1/my_account/oauth2/token'
payload = 'username=my_username&password=my_password&grant_type=password'

headers = {'Content-Type': 'application/x-www-form-urlencoded'}

response = requests.request('POST', url, headers = headers, data = payload, allow_redirects=False)
print(response.text)

{"access_token":"my_access_token","token_type":"bearer","expires_in":43199,"user_name":"my_username","roles":"System Administrator,Contact Administrator,Survey Administrator","account_name":"my_account","hosts":"my_account.checkboxonline.com"}

我尝试使用 httr 执行相同的操作,但出现 400 错误。

username <- "my_username"
password <- "my_psw"
base_url = "https://api.checkbox.com/v1/my_account/oauth2/token"

response <- POST(url=base_url,
                   authenticate(username,password),
                   add_headers("Content-Type"="application/x-www-form-urlencoded"),verbose())

这里是生成的请求参数:

-> POST /v1/my_account/oauth2/token HTTP/1.1
-> Host: api.checkbox.com
-> Authorization: Basic jumble_hog_wash_of_my_creds
-> User-Agent: libcurl/7.64.1 r-curl/4.1 httr/1.4.1
-> Accept-Encoding: deflate, gzip
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Content-Length: 0
-> 
<- HTTP/1.1 400 Bad Request
<- Cache-Control: no-cache
<- Pragma: no-cache
<- Content-Type: application/json;charset=UTF-8
<- Expires: -1
<- Server: Microsoft-IIS/10.0
<- X-Powered-By: ASP.NET
<- Date: Fri, 08 Nov 2019 16:08:10 GMT
<- Content-Length: 34
<- 
Response [https://api.checkbox.com/v1/my_account/oauth2/token]
  Date: 2019-11-08 16:08
  Status: 400
  Content-Type: application/json;charset=UTF-8
  Size: 34 B

我不确定如何通过请求传递 &'grant_type=password'。有什么想法吗?

来自使用grant_type = "password"API DOC your API is supporting Oauth2 authentication using Resource Owner Password Credentials Grant

对于 Oauth2 API 你通常会使用 httr::oauth2.0_token 但目前 httr Oauht2 流程不支持这种类型的授权。

你需要自己做。

从文档(我没有尝试,因为我无法访问此 API),您需要使用 POST 和包含您的参数的 body。

这应该有效:

username <- "my_username"
password <- "my_psw"
base_url = "https://api.checkbox.com/v1/my_account/oauth2/token"

response <- POST(url=base_url,
                 # pass data to the POST request
                 body = list(
                   username = username,
                   password = password,
                   grant_type = "password"
                 ),
                 # application/x-www-form-urlencoded
                 encode = "form",
                 verbose())

请参阅使用 httpbin.org 作为示例,您将获得与示例中相同的 header,您的数据传递为 POST

中的表单
username <- "my_username"
password <- "my_psw"

response <- httr::POST(
  # for demo
  url= "https://httpbin.org/post", 
  # pass data to the POST request
  body = list(
    username = username,
    password = password,
    grant_type = "password"
  ),
  # application/x-www-form-urlencoded
  encode = "form")

jsonlite::prettify(httr::content(response, "text"))
#> No encoding supplied: defaulting to UTF-8.
#> {
#>     "args": {
#> 
#>     },
#>     "data": "",
#>     "files": {
#> 
#>     },
#>     "form": {
#>         "grant_type": "password",
#>         "password": "my_psw",
#>         "username": "my_username"
#>     },
#>     "headers": {
#>         "Accept": "application/json, text/xml, application/xml, */*",
#>         "Accept-Encoding": "deflate, gzip",
#>         "Content-Length": "56",
#>         "Content-Type": "application/x-www-form-urlencoded",
#>         "Host": "httpbin.org",
#>         "User-Agent": "libcurl/7.64.1 r-curl/4.2.9000 httr/1.4.1"
#>     },
#>     "json": null,
#>     "origin": "176.158.63.46, 176.158.63.46",
#>     "url": "https://httpbin.org/post"
#> }
#> 

reprex package (v0.3.0)

于 2019-11-08 创建

希望对您有所帮助