这有效:
requests.get('https://abc.123.xyz.xom/getmethisurl',
auth=('PasswordIsAuthToken', 'khcdhk-dcbdmsb-dcbdsm-aBSDCXKN'),
verify=False)
这并不是每次都获得401:
import json
import requests
from requests.auth import HTTPBasicAuth
class connect(object):
def __init__(self, url, user, token):
self.url=url
self.user=user
self.token=token
def uget(self, uri):
self.url = self.url + uri
## EDIT 1 : Code added to print out the parameters values and type for debugging
print ("IN:", repr(self.url), repr(self.user), repr(self.token))
print(self.url, type(self.url))
print(self.user, type(self.user))
print(self.token, type(self.token))
res=requests.get(self.url, auth=(self.user, self.token), verify=False)
if res.status_code == 401:
print ("ERORR 401 !!!!")
else:
return res
import defconn
from connexion.resolver import RestyResolver
props = dict(line.strip().split('=') for line in open('env.properties'))
url=props['connect.url']
user=props['connect.username']
token=props['connect.token']
oj=defconn.connect(url,user,token)
oj.uget('/getmethisurl')
这是我的env.properties:
connect.url=https://abc.123.xyz.xom
connect.username=PasswordIsAuthToken
connect.token=khcdhk-dcbdmsb-dcbdsm-aBSDCXKN
非常感谢任何帮助。
编辑1:添加诊断代码及其输出屏幕截图
谢谢 - A
答案 0 :(得分:0)
可能的原因是参数的某个地方与简单的硬编码示例不匹配。尝试添加诊断线,以便隔离问题:
class connect(object):
def __init__(self, url, user, token):
self.url=url
self.user=user
self.token=token
def uget(self, uri):
self.url = self.url + uri
print(repr(self.url), repr(self.user), repr(self.token)) # <== DIAGNOSTIC
res=requests.get(self.url, auth=(self.user, self.token), verify=False)
if res.status_code == 401:
print ("ERORR 401 !!!!")
else:
return res
正如评论中所述,self.url = self.url + uri
可能并不是您想要的。相反,只需更新一个局部变量。