我的C# REST API被AngularJS web应用程序调用。我通过验证用户身份并确保用户是特定Windows组的一部分来保护Web API。
现在客户希望从脚本(Python)调用API。我该如何实现?我应该让他们在json调用中传递用户名和密码吗?
现在客户希望从脚本(Python)调用API。我该如何实现?我应该让他们在json调用中传递用户名和密码吗?
Authorization bearer token
或类似的标头中。或者,如果您喜欢,您可以在cookie中保存令牌以保持向后兼容性。请使用您当前正在使用的完全相同的身份验证方法。
以下是使用Python的基本示例(未经测试):
from requests.auth import HTTPBasicAuth
s = requests.Session()
# Make the initial authentication request from a session object
s.get('https://omg.wtf/user', auth=HTTPBasicAuth('user', 'pass'))
# All subsequent requests from that session will include any cookies set in the initial response
r = s.get('http://omg.wtf/911')
print(r.text)