简体   繁体   English

将Python REST API身份验证脚本转换为curl命令

[英]Convert Python REST API authentication script to curl command

i've made successful authentication with the following python script to a REST API: 我已经使用以下python脚本对REST API进行了成功的身份验证:

import requests, base64
import json

VendorId = "a"
VendorPassword = "b"
UserId = "c"
UserPassword = "d"
PropertyId = "e"
authurl = "https://url/api/auth"
usrPass = '' + VendorId + ":" + VendorPassword

b64Val = base64.b64encode(usrPass)
payload = { "UserId" : UserId, "UserPassword" : UserPassword, "PropertyId" : PropertyId}
r = requests.post(authurl, headers = {"Authorization":b64Val}, data = payload)

and i want to do it through curl (completely inexperienced). 我想通过卷曲(完全没有经验)做到这一点。 What i've figured out until now is the following: 到目前为止,我发现的是以下内容:

curl -H "{'Authorization': 'abCdEfgHijKlmnop=='}" -X POST -v "https://url/api/auth" -d "batch={'PropertyId': 'e', 'UserId': 'c', 'UserPassword': 'd'}";

but i get the following 502 error from the REST API... 但是我从REST API中收到以下502错误...

If i don't use a header i have the following error: 如果我不使用标题,则会出现以下错误:

< X-SERVICENAME-Auth-Error: Missing vendor credentials in HTTP auth header <X-SERVICENAME-Auth-Error:HTTP身份验证头中缺少供应商凭据

I just want an insight on what i'm missing here, does the header syntax is wrong, or maybe the curl arguments misplaced? 我只想了解我在这里缺少的内容,标题语法是否错误,或者curl参数放错了位置?

Thanks in advance for any idea/tutorial/advice! 在此先感谢您的任何想法/指导/建议!

Your header setting is wrong, it will be: 您的标题设置错误,它将是:

-H "Authorization: abCdEfgHijKlmnop=="

Also, if you have the authorization user:password then you can use: 另外,如果您具有授权user:password,则可以使用:

-u user:password

You already solved this in python and wrote a script that generates a successful request. 您已经在python中解决了此问题,并编写了一个生成成功请求的脚本。 I would definitely start from there. 我一定会从那里开始。 Since you only need a single request, comparing their outputs should suffice for a trivial protocol such as HTTP. 由于只需要一个请求,因此比较它们的输出就可以满足诸如HTTP之类的简单协议。

Try modifying the above scripts to make requests to 127.0.0.1 and some arbitrary ports as shown below. 尝试修改上面的脚本以向127.0.0.1和一些任意端口发出请求,如下所示。 Adapt this to your preferred tools: 使其适应您的首选工具:

ncat --recv-only -l 8080 > desired &
ncat --recv-only -l 8081 > in_question &
python your_desired_auth_script.py 127.0.0.1 8080
curl 127.0.0.1:8081/api/auth "parameters not resulting in authorization..."
vimdiff desired in_question

Just modify your python script to accept host and port arguments or hardcode them. 只需修改python脚本以接受主机和端口参数或对其进行硬编码即可。 Don't forget the request path. 不要忘记请求路径。 If you don't instantly spot the mistake, reconstruct the exact request with curl while reading its documentation. 如果您没有立即发现错误,请在阅读其文档的同时使用curl重建确切的请求。 After succeeding you can incrementally deviate until you find which difference causes it. 成功之后,您可以逐渐偏离,直到找到导致差异的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM