Tôi đã cố gắng gửi một yêu cầu nghỉ ngơi trong Python với xác thực dựa trên chứng chỉ cho một máy chủ nhất định đang cung cấp API REST nhưng sau nhiều giờ tìm kiếm và thử tôi nghĩ rằng tôi cần trợ giúp.
Tôi có một chứng chỉ đã ký từ máy chủ được đề cập và chìa khóa cho chứng chỉ đó. Bản thân máy chủ cũng cung cấp chứng chỉ cho HTTPS.
Tôi đã thử thư viện httplib.httpsconnection:
import httplib import urllib clientCert = "client.crt" clientKey = "client.key" serverCert = 'server.crt' serverApi = "/api/getExample" serverHost = "restapi.example.com" serverPort = 443 requestMethod = "POST" params = urllib.urlencode[{'someId': 'myId'}] headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "application/json"} conn = httplib.HTTPSConnection[serverHost, serverPort, key_file=clientKey, cert_file=clientCert] conn.request[requestMethod, serverApi, params, headers] response = conn.getresponse[] conn.getresponse[] conn.close[]
Tôi nhận được ssl.SSLError: SSL: CERTIFICATE_VERIFY_FAILED
có thể có được một chứng thực dựa trên chứng chỉ với thư viện đó không?
Is it possible to get a cert based auth running with that library?
Hỏi ngày 17 tháng 8 năm 2018 lúc 8:37Aug 17, 2018 at 8:37
Tôi đã có thể chạy nó với sự trợ giúp của thư viện "yêu cầu".
import json
import requests
clientCrt = "cc.crt"
clientKey = "ck.key"
url = "//example.com/api"
payload = { "someId": "myID" }
certServer = 'cs.crt'
headers = {'content-type': 'application/json'}
r = requests.post[url, data=json.dumps[payload], verify=certServer,
headers=headers, cert=[clientCrt, clientKey]]
print[r.status_code]
print[r.json[]]
Đơn giản như thế
Đã trả lời ngày 17 tháng 8 năm 2018 lúc 10:01Aug 17, 2018 at 10:01
3
Chào Vikas,
Chúng tôi có cấu hình này cho JAMA và có thể truy cập API REST qua Python mà không gặp vấn đề gì. & NBSP; Một khối mã khá đơn giản để truy xuất danh sách dự án bằng Python 2.7 dưới đây:
import requests
import json
from creds import creds
myCreds = creds[]URL = '//yoururl.yourcompany.com'
cert = 'C:\Certs\yoururl.b64.cer'startAt = 0
maxResults = 20
done = Falsewhile not done:
try:
response = requests.get[
URL + ':443/contour/rest/latest/projects',
auth=[myCreds.userName,myCreds.password],
params={'startAt':startAt,'maxResults':maxResults},
verify=cert]
try:
response.raise_for_status[]
except requests.exceptions.HTTPError as e:
print "HTTPError raised:", str[e]
# Check for the type of error
# example: 401 Unauthorized means credentials are wrong; log error and exit
# example: 404 Not Found means call is invalid; log error and exit
projects = response.json[]pageInfo = projects['meta']['pageInfo']
if [pageInfo['startIndex']+pageInfo['resultCount'] < pageInfo['totalResults']]:
startAt = startAt + maxResults
done = False
else:
done = Truefor project in [projects["data"]]:
if project.get['isFolder'] == False:
print[project['fields']['name']+'\t'+
project['fields']['projectKey']+'\t'+
str[project['id']]]except requests.exceptions.ConnectionError as e:
print "Connection address does not exist:", str[e]
# Can wait and retry, but if address is actually wrong the retries should be limitedexcept requests.exceptions.ConnectTimeout as e:
print "Connection timeout:", str[e]
# Can wait and retry a few times, but should limit the # of retries to 2 or 3
Mô -đun tín dụng là một mô -đun được tạo cục bộ, đơn giản là cung cấp tên người dùng và mật khẩu; Hai mô -đun còn lại là một phần của cài đặt Python hoặc dễ dàng có được thông qua PIP. & NBSP; Tệp CERT có thể được tạo bằng cách xuất chuỗi chứng nhận từ trình duyệt của bạn và kết hợp các certs riêng lẻ vào một tệp CERT [một số ví dụ trực tuyến]. Tệp chứng nhận có thể hoặc không cần thiết; Hãy thử mà không cần tham số xác minh = chứng chỉ trong cuộc gọi yêu cầu. Gọi.
Mike