首页 > 解决方案 > 发送登录标头 python 2.7

问题描述

嗨,我需要将登录标头发送到 sigfox。但是,当我在调用中插入 auth 变量时,它们总是被拒绝或代码失败。

这是有效但拒绝的代码。

print 'end of the dummy'
devices = requests.get("https://api.sigfox.com/v2/devices", auth=(API_USER, API_PWD)).json()
r = requests.get('https://api.sigfox.com/v2/devices', data=users, stream=True)
data = r.raw.read(r)

打印(数据)

这是失败的代码。

credentials = b64encode(b"somebase64string:somebase64string").decode("ascii")
headers = { 'Authorization' : 'Basic %s' %  credentials }


print 'end of the dummy'
devices = requests.get("https://api.sigfox.com/v2/devices", auth=(API_USER, API_PWD)).json() r = requests.get('https://api.sigfox.com/v2/devices', auth=credentials, data=users, stream=True) data = r.raw.read(r)


print(data) 

第二个代码不那么混乱,但 stackoverflow 可以使用 idention。我想我需要在 r 变量中传递身份验证,但我无法弄清楚我是如何正确地做到这一点的。我正在使用 Python 2.7

标签: pythonjsonapiiot

解决方案


您需要对用户名和密码进行编码。

encodedUname = urllib.quote_plus(userName)
encodedPwd = urllib.quote_plus(password)
http://{encodedUname}:{encodedPwd}@{url}

推荐阅读