python - 发送登录标头 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
解决方案
您需要对用户名和密码进行编码。
encodedUname = urllib.quote_plus(userName)
encodedPwd = urllib.quote_plus(password)
http://{encodedUname}:{encodedPwd}@{url}
推荐阅读
- java - 如何使用 ORMLite 在 SQLite 中持久化 JavaFX 属性?
- php - 无法将 404、403 和 401 重定向到自定义页面 (loader.php)
- sql-server - 获取 SQL Server 中的所有表、视图和存储过程
- ruby-on-rails - 如何在rails中获得模型的前5名?
- wso2 - WSO2 EI 6.6.0 类调解器无法使用 SOAP 调用返回内容
- python - 将第 3 方 json 反序列化为 python 类对象
- sql - SQL,Postgres:如何从非正态分布中获得百分位数?
- ruby-on-rails - NameError(未初始化的常量 SnowplowTracker::SelfDescribingJson):在 ruby 中制作扫雪机跟踪器时
- vba - “编译错误:参数不是可选的”尝试在 VBA 中检索 Shape.ScaleHeight 值时出错
- reactjs - 如何防止用户在注销后返回主屏幕