Python: 请求: post: 基本授权
Python: requests: post: basic authorization
我有一个 python 函数,它使用请求接收参数和 posts 到 api 端点,基本 authorization.The post 工作正常在邮递员中;但是,在 python 中,我收到 401 错误(身份验证失败)。下面是我的代码:
request_body = '{"account": "%s"' % token
request_body += ', "expiry": "%s"'% expiry_mmyy
request_body += ', "name": "%s"' % name
request_body += ', "merchid": "%s"' % auth_merchant_id
request_body += ', "amount": "%s"' % amount
request_body += ', "tokenize": "y" '
request_body += ', "orderId": "%s"' % reference_number
request_body += ', "capture": "y"}'
json_string = json.dumps(request_body)
#commented this line out and it started working
#json_string = json_string[1:-1]
logger.info('JSON Request: %s' % json_string)
logger.info('Username: %s' % AUTH_USER)
logger.info('Password: %s' % AUTH_PASSWORD)
http_headers = {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": str(len(json_string)),
"Authorization": "Basic"
}
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
#response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=("%s" % AUTH_USER, "%s" % AUTH_PASSWORD))
#response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=('testing','testing123'))
#response = requests.post(CC_URL, verify=False, headers=http_headers, data={json_string}, auth=HTTPBasicAuth("%s" % AUTH_USER,"%s" % AUTH_PASSWORD))
response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=HTTPBasicAuth('testing', 'testing123'))
if response.status_code == 200:
logger.info('POST Success (HTTP code 200)')
#response_string = response.json
response_string = response.text
logger.info('JSON Response: %s' % response_string)
logger.info(response_string['resptext'])
logger.info(response_string['authcode'])
else:
logger.info("Error Code %s" % response.status_code)
我不确定基本身份验证是否有效。感谢任何帮助。
可能是TLS1.2的问题;我不确定如何将上下文传递给 requests.post?
安装的 python 版本是 2.7,ssl 版本是 1.0.2k。
谢谢
编辑:
我尝试使用 urllib2.urlopen 对其进行测试,但仍然出现相同的错误。我联系了另一端的人,他们说,他们的日志显示 http headers 丢失了。而且,他看不到目标 URL.
您不必像 header 那样传递“授权”:“基本”部分。只是 requests.post()
上的 auth=HTTPBasicAuth('testing', 'testing123') 部分
这是一行:
json_string = json_string[1:-1]
我注释掉了这一行,它开始工作了
我有一个 python 函数,它使用请求接收参数和 posts 到 api 端点,基本 authorization.The post 工作正常在邮递员中;但是,在 python 中,我收到 401 错误(身份验证失败)。下面是我的代码:
request_body = '{"account": "%s"' % token
request_body += ', "expiry": "%s"'% expiry_mmyy
request_body += ', "name": "%s"' % name
request_body += ', "merchid": "%s"' % auth_merchant_id
request_body += ', "amount": "%s"' % amount
request_body += ', "tokenize": "y" '
request_body += ', "orderId": "%s"' % reference_number
request_body += ', "capture": "y"}'
json_string = json.dumps(request_body)
#commented this line out and it started working
#json_string = json_string[1:-1]
logger.info('JSON Request: %s' % json_string)
logger.info('Username: %s' % AUTH_USER)
logger.info('Password: %s' % AUTH_PASSWORD)
http_headers = {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": str(len(json_string)),
"Authorization": "Basic"
}
ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
#response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=("%s" % AUTH_USER, "%s" % AUTH_PASSWORD))
#response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=('testing','testing123'))
#response = requests.post(CC_URL, verify=False, headers=http_headers, data={json_string}, auth=HTTPBasicAuth("%s" % AUTH_USER,"%s" % AUTH_PASSWORD))
response = requests.post(url=CC_URL, headers=http_headers, data=json_string, auth=HTTPBasicAuth('testing', 'testing123'))
if response.status_code == 200:
logger.info('POST Success (HTTP code 200)')
#response_string = response.json
response_string = response.text
logger.info('JSON Response: %s' % response_string)
logger.info(response_string['resptext'])
logger.info(response_string['authcode'])
else:
logger.info("Error Code %s" % response.status_code)
我不确定基本身份验证是否有效。感谢任何帮助。
可能是TLS1.2的问题;我不确定如何将上下文传递给 requests.post?
安装的 python 版本是 2.7,ssl 版本是 1.0.2k。
谢谢
编辑: 我尝试使用 urllib2.urlopen 对其进行测试,但仍然出现相同的错误。我联系了另一端的人,他们说,他们的日志显示 http headers 丢失了。而且,他看不到目标 URL.
您不必像 header 那样传递“授权”:“基本”部分。只是 requests.post()
上的 auth=HTTPBasicAuth('testing', 'testing123') 部分这是一行: json_string = json_string[1:-1]
我注释掉了这一行,它开始工作了