我有以下参数可以通过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'
与请求一起传递。有什么想法吗?
答案 0 :(得分:1)
从API DOC开始,您的API支持使用grant_type = "password"
的{{3}}进行Oauth2身份验证。
对于Oauth2 API,您通常会使用httr::oauth2.0_token
,但是目前httr
Oauht2流不支持这种类型的授权。
您需要自己做。
在文档中(我没有尝试过,因为我无法访问此API),您需要使用POST
,并将其正文包含您的参数。
这应该有效:
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作为示例,您将获得与示例中相同的标头,并且数据以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"
#> }
#>
由Resource Owner Password Credentials Grant(v0.3.0)于2019-11-08创建
希望有帮助