简体   繁体   English

如何通过 Requests 和 Python 查询和登录 Bill.com API?

[英]How to query and login into the Bill.com API via Requests and Python?

I'd like to query and login into Bill.com via their REST API .我想通过他们的 REST API查询和登录Bill.com In the past the below code worked, but now I'm getting a new error, which I'm unsure how to fix based on the Bill.com API Login documentation .过去,下面的代码可以工作,但现在我遇到了一个新错误,根据Bill.com API 登录文档,我不确定如何修复。

login_end_point='https://api.bill.com/api/v2/Login.json?'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.get(login_end_point, params=login_params_dict).json()

In the past, this use to work perfectly, but now I' receiving the following error code:过去,这可以完美运行,但现在我收到以下错误代码:

{u'response_status': 1, u'response_message': u'Error', u'response_data': {u'error_message': u'HTTP Method not supported.', u'error_code': u'BDC_1340'}}

I've also tried the following, but I still receive an error:我也尝试了以下方法,但仍然收到错误消息:

Trying different login_end_point:尝试不同的 login_end_point:

login_end_point='https://api.bill.com/api/v2/'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.get(login_end_point, params=login_params_dict).json()

Trying login_end_point and post instead of get:尝试 login_end_point 和 post 而不是 get:

login_end_point='https://api.bill.com/api/v2/'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.post(login_end_point, params=login_params_dict).json()

Both of these tries return this error:这两种尝试都返回此错误:

{u'response_status': 1, u'response_message': u'Error', u'response_data': {u'error_message': u'API not supported.', u'error_code': u'BDC_1121'}}

I've also tried the following:我还尝试了以下方法:

login_end_point= 'https://api.bill.com/api/v2/Login.json'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.post(login_end_point, params=login_params_dict).json()

but this returns the following error:但这会返回以下错误:

{u'response_status': 1, u'response_message': u'Error', u'response_data': {u'error_message': u'Invalid request. Check query string parameters.', u'error_code': u'BDC_1339'}}

Finally, I've tried the following using http instead of https最后,我尝试了以下使用 http 而不是 https

login_end_point= 'http://api.bill.com/api/v2/Login.json'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.post(login_end_point, params=login_params_dict).json()

But this returns the following error:但这会返回以下错误:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='api.bill.com', port=80): Max retries exceeded with url: /api/v2/Login.json?userName=%40Cue.com&devKey=01NZ&password=DoC&orgId=00(Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x106602b90>: Failed to establish a new connection: [Errno 60] Operation timed out',))

As per the documentation you linked:根据您链接的文档:

Do not send the username and password as query parameters as it is not secure, ie, don't pass your parameters as part of the URL ( /Login.json?userName=X&password=Y) nor pass the parameters as GET request.不要将用户名和密码作为查询参数发送,因为它不安全,即不要将参数作为 URL 的一部分(/Login.json?userName=X&password=Y)传递,也不要作为 GET 请求传递参数。

Instead, use POST instead of GET and store the parameters in the request body.相反,使用 POST 而不是 GET 并将参数存储在请求正文中。

For all Login calls, GET requests will be rejected as well as any POST requests that contain parameters in the URL instead of the request body.对于所有登录调用,GET 请求以及在 URL 中而不是请求正文中包含参数的任何 POST 请求都将被拒绝。

You need to do a POST request and store login data in the request body.您需要执行 POST 请求并将登录数据存储在请求正文中。

In your code, you should pass login data as the json argument instead of params :在您的代码中,您应该将登录数据作为json参数而不是params传递:

login_end_point= 'https://api.bill.com/api/v2/Login.json'
login_params_dict = {'userName': username, 'password': password, 'orgId': orgId, 'devKey': devKey}
result = requests.post(login_end_point, json=login_params_dict)

Note that if you're using an older version of requests (< 2.4.2), you should encode the data dict yourself:请注意,如果您使用的是旧版本的请求(< 2.4.2),您应该自己编码数据字典:

result = requests.post(login_end_point, data=json.dumps(login_params_dict))

Requests docs 请求文档

不要忘记作为formdata而不是json正文发送!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM