从python到HTTR

问题描述 投票:0回答:1

我有以下参数可以通过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'。有什么想法吗?

我有以下参数可以通过python库(网状)导入请求在api上对我进行身份验证url ='https://api.checkbox.com/v1/my_account/oauth2/token'有效负载='username =。 。

python r httr reticulate
1个回答
0
投票

API DOC开始,您的API支持使用Resource Owner Password Credentials Grantgrant_type = "password"使用Oauth2身份验证。

© www.soinside.com 2019 - 2024. All rights reserved.