[英]Postman API request in Python
I am trying to web-scrape data from https://www.mcmaster.com .我正在尝试从https://www.mcmaster.com中抓取数据。 They have provided me with a.pfx file and a passphrase.
他们为我提供了一个 .pfx 文件和一个密码。 When making a GET request on Postman using their.json file, I input my website login/password and upload the.pfx certificate with its passphrase and everything works fine.
当使用他们的.json 文件对 Postman 发出 GET 请求时,我输入了我的网站登录名/密码并上传了带有密码的 .pfx 证书,一切正常。 Now I am trying to do this same thing but in Python, but am a bit unsure.
现在我正在尝试做同样的事情,但在 Python 中,但有点不确定。
Here is my current Python code, I am unsure where I would put the website email/password login and how to successfully do a GET request.这是我当前的 Python 代码,我不确定将网站电子邮件/密码登录放在哪里以及如何成功执行 GET 请求。
import requests_pkcs12
from requests_pkcs12 import get
r = get('https://api.mcmaster.com/v1/login', pkcs12_filename='Schallert.pfx', pkcs12_password='mcmasterAPI@1901')
response = requests_pkcs12.get(r)
print(response.text)
Here is how I have it setup in Postman (Website email/pw login)这是我在 Postman 中设置的方法(网站电子邮件/密码登录)
Postman has a built in feature where it will convert requests into code. Postman 有一个内置功能,可以将请求转换为代码。 You can do it like so:
你可以这样做:
One thing I'll warn you about though is the URL.
我要警告你的一件事是 URL。 Postman doesn't add http:// or https:// to the URL, which means Python will throw a No Scheme Supplied error.
Postman doesn't add http:// or https:// to the URL, which means Python will throw a No Scheme Supplied error.
Available Packages for Auto Conversion:自动转换的可用软件包:
Meaning you will have to use a different package instead of requests_pkcs12
这意味着您将不得不使用不同的 package 而不是
requests_pkcs12
After a quick web search, it looks like you need to create a temporary certificate as a .pem
file, which is then passed to the request.在快速搜索 web 之后,您似乎需要创建一个临时证书作为
.pem
文件,然后将其传递给请求。
from contextlib import contextmanager
from pathlib import Path
from tempfile import NamedTemporaryFile
import requests
from cryptography.hazmat.primitives.serialization import Encoding, PrivateFormat, NoEncryption
from cryptography.hazmat.primitives.serialization.pkcs12 import load_key_and_certificates
@contextmanager
def pfx_to_pem(pfx_path, pfx_password):
pfx = Path(pfx_path).read_bytes()
private_key, main_cert, add_certs = load_key_and_certificates(pfx, pfx_password.encode('utf-8'), None)
with NamedTemporaryFile(suffix='.pem') as t_pem:
with open(t_pem.name, 'wb') as pem_file:
pem_file.write(private_key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()))
pem_file.write(main_cert.public_bytes(Encoding.PEM))
for ca in add_certs:
pem_file.write(ca.public_bytes(Encoding.PEM))
yield t_pem.name
with pfx_to_pem('your pfx file path', 'your passphrase') as cert:
requests.get(url, cert=cert, data=payload)
The package requests_pkcs12
is a wrapper written above the requests
package. package
requests_pkcs12
是写在requests
package 之上的包装器。 So, all the parameters that accept requests
will accept by the requests_pkcs12
.因此,所有接受
requests
的参数都将被requests_pkcs12
接受。
Here is the source code proof for that.这是源代码证明。 https://github.com/m-click/requests_pkcs12/blob/master/requests_pkcs12.py#L156
https://github.com/m-click/requests_pkcs12/blob/master/requests_pkcs12.py#L156
Also, from your screenshots, I understood that you are using POST
not GET
.另外,从您的屏幕截图中,我了解到您使用的是
POST
而不是GET
。
import json
from requests_pkcs12 import post
url = "https://api.mcmaster.com/v1/login"
payload = {'Username': 'yourusername',
'Password': 'yourpassword'}
resp = post(url, pkcs12_filename='Schallert.pfx', pkcs12_password='mcmasterAPI@1901', data=json.dumps(payload))
print(resp)
Footnote: I hope the password mcmasterAPI@1901
it's a fake one.脚注:我希望密码
mcmasterAPI@1901
是假的。 if not please don't share any credentials in the platform.如果不是,请不要在平台上分享任何凭据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.