python用户名密码访问rest接口_如何使用密钥(而不是基本身份验证用户名和密码)将Python与Python连接到RESTful API?...

本文介绍了如何将Python代码更新以适应一个RESTful API的版本2,该版本要求使用ID和Key进行认证,而非基本的用户名和密码。作者提供了从cURL命令转换为Python请求的方法,通过设置HTTP头来包含必要的认证信息。
摘要由CSDN通过智能技术生成

I am new to programming, and was asked to take over a project where I need to change the current Python code we use to connect to a Ver 1 RESTful API. The company has switched to their Ver 2 of the API and now require IDs and Keys for authentication instead of the basic username and password. The old code that worked for the Ver 1 API looks like this:

import requests

import simplejson as json

import pprintpp as pprint

#API_Ver1 Auth

USER = 'username'

PASS = 'password'

url = 'https://somecompany.com/api/v1/groups'

s = requests.Session()

s.auth = (USER, PASS)

r = json.loads(s.get(url).text)

groups = r["data"]

I can connect to the Ver 2 API via a terminal using a cURL string like this:

curl -v -X GET -H "X-ABC-API-ID:x-x-x-x-x"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值