这个API如何连接?

问题描述 投票:0回答:1

我正在尝试通过 API 进行连接。

客户向我发送了 2 个 URL(测试和生产)、参数、客户端 ID、订阅(Ocp-Appim-Subscription-Key 参数)和客户端密钥

我尝试使用下面的代码使用 Anaconda + Python Jupyter Notebooks,但它不起作用(我以前从未这样做过,也许有更好的使用方法)

我不确定我是否使用了正确的连接方式。我应该使用任何 azure 工具还是其他工具?

代码:

import requests

# API endpoint
url = "https://apim-genericapi-test.azure-api.net/GenericAPI/v1/"

# Authentication details
subscription_key = "xxxxxxxx"
client_secret = "xxxxxxxxxx"
client_id = "xxxxxxxxx"

# Request headers
headers = {
    "Ocp-Apim-Subscription-Key": subscription_key,
    "Content-Type": "application/json"
}

# Authentication payload
payload = {
    "client_id": client_id,
    "client_secret": client_secret,
    "grant_type": "client_credentials"
}

# Requesting access token
token_url = "https://login.microsoftonline.com/common/oauth2/token"
try:
    token_response = requests.post(token_url, data=payload)
    token_data = token_response.json()
    access_token = token_data.get("access_token")
    if not access_token:
        print("Failed to retrieve access token.")
        print("Response:", token_data)
        print("Request:", token_response.request.body)  # Print request body for debugging
        print("Response status code:", token_response.status_code)  # Print response status code
        exit()
except Exception as e:
    print("Error occurred while fetching access token:", e)
    exit()

# Making authenticated API request
try:
    response = requests.get(url, headers={"Authorization": "Bearer " + access_token})
    response.raise_for_status()  # Raise an exception for 4xx or 5xx status codes
    print(response.json())  # Print or handle the response data as needed
except requests.exceptions.HTTPError as err:
    print("HTTP Error:", err)
except Exception as e:
    print("Error occurred while making API request:", e)
python azure-api-management apim
1个回答
0
投票

我对您的代码做了一些修改,并在向 APIM 端点发出请求时添加了所有必需的标头。我同意 Vlad DX,您需要从 API 调用特定操作,就像我使用以下操作进行测试一样。

enter image description here

此操作给出以下输出。

enter image description here

我正在使用给定的代码来调用 Echo API。

import requests

# API endpoint
url = "https://{apim-instance-name}.azure-api.net/echo/resource?param1=sample"

# Authentication details
subscription_key = "bd7b******1c1d04"
client_secret = "f-h8Q~*******rrLjbBM"
client_id = "52c**********818"

# Request headers
headers = {
    "Ocp-Apim-Subscription-Key": subscription_key,
    "Content-Type": "application/json"
}

# Authentication payload
payload = {
    "client_id": client_id,
    "client_secret": client_secret,
    "grant_type": "client_credentials"
}

# Requesting access token
token_url = "https://login.microsoftonline.com/{tenant_id}/oauth2/token"
try:
    token_response = requests.post(token_url, data=payload)
    token_data = token_response.json()
    access_token = token_data.get("access_token")
    if not access_token:
        print("Failed to retrieve access token.")
        print("Response:", token_data)
        print("Request:", token_response.request.body)  
        print("Response status code:", token_response.status_code)  
        exit()
except Exception as e:
    print("Error occurred while fetching access token:", e)
    exit()
    
# Combine headers
combined_headers = {
    **headers,
    "Authorization": "Bearer " + access_token
}

# Making authenticated API request
try:
    response = requests.get(url, headers=combined_headers)
    response.raise_for_status()  
    print(response.json())  
except requests.exceptions.HTTPError as err:
    print("HTTP Error:", err)
except Exception as e:
    print("Error occurred while making API request:", e)

我能够得到预期的回应。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.