3

所以我使用 FreshDesk API 并能够使用请求模块获取响应,但是每当我使用代理服务器时,我都无法获得响应。

import base64
import requests
import os
from requests.auth import HTTPBasicAuth
import ssl
method = "get"
url = "https://mycompanydomain.freshdesk.com/api/v2/tickets"
apiKey = "XXXXXXXXX"
secret = "x"
os.environ["REQUESTS_CA_BUNDLE"] = "Path to CA Certs"
auth = HTTPBasicAuth(apiKey, secret)
rsp = requests.request(method, url, headers=None, auth=auth)
print(rsp.text)

但是每当我在我的组织中使用代理服务器时,我都会收到一条错误消息,例如{"code":"invalid_credentials","message":"您必须登录才能执行此操作。"}

我用于代理服务器的代码

import base64
import requests
import http.client
import urllib.parse
method = "get"
apiKey = "XXXXXXXX"
secret = "x"
url = "https://mycompanydomain.freshdesk.com/api/v2/tickets"
cred= '{}:{}'.format(apiKey, secret)
cred =  base64.b64encode(cred.encode('utf-8')).decode('utf-8')
authorization_headers = {
        'Proxy-Authorization': 'Basic {}'.format(cred)
}
conn = http.client.HTTPSConnection("11.125.250.121", 3128)
conn.set_tunnel("mycompanydomain.freshdesk.com", headers = authorization_headers)
headers = { 'Content-Type' : 'application/json' }
conn.request("GET", "/api/v2/tickets",headers = headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

FreshDesk API Docs 使用他们的 API

curl -v -u abcdefghij1234567890:X -H "Content-Type: application/json" -X GET 'https://domain.freshdesk.com/api/v2/tickets'

有什么可能的方法来解决这个错误?

4

1 回答 1

0

这是一个猜测,我实际上没有尝试过,但我确实使用了 Freshdesk,我应该使用他们的 API。

这是 Freshdesk API 的链接:https: //support.freshdesk.com/support/solutions/articles/216548-create-and-update-tickets-with-custom-fields-using-api

我会尝试取出“-H”Content-Type: application/json”以更好地匹配建议的代码。在大多数情况下,我会为 POSTS 而不是 GETs 添加内容类型,除非 API 专门要求它。试试看让我们知道它是如何工作的。

curl -u API_KEY:X -X GET https://domain.freshdesk.com/api/v2/ticket_fields
于 2021-08-31T23:20:50.247 回答