Connecting to API - need azure tool?

Rezende, Fernando SBRASEP-STS/631 0 Reputation points
2024-05-16T00:59:10.03+00:00

Hello Guys,

I need to connect to an API of my client. It is accessible via secret and subscription key authentication to generate a token.

I have these 2 URLS:

Test: https://apim-genericapi-test.azure-api.net/GenericAPI/v1/Trip

Prod: https://apim-genericapi-prod.azure-api.net/GenericAPI/v1/Trip

Also, the client provided me:

Parameter, Client ID, Subscription (Ocp-Appim-Subscription-Key Parameter) and Client Secret

I was trying using Anaconda + Jupyter Notebooks using the code below but it is not working. My apologies, it is my first time doing this. Do I need any Azure Tool or APIM set up? My final goal is to have this data in Power Bi.

Many thanks!

import requests

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

# Authentication details
subscription_key = "xxxx"
client_secret = "xxxxx"
client_id = "xxxxx"

# 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)
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,220 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JananiRamesh-MSFT 29,191 Reputation points
    2024-05-16T12:15:18.12+00:00

    @Rezende, Fernando SBRASEP-STS/631 Thanks for reaching out. The code given above is a common way to authenticate and make requests to an API. However, there are a few things you might want to check:

    Ensure that the client_id, client_secret, and subscription_key is correct. These are sensitive pieces of information that need to be accurate for the authentication to work.

    Make sure the endpoint you’re using is correct.

    Token URL ("https://login.microsoftonline.com/common/oauth2/token") which is used to get the token might vary based on the tenant ID. Please confirm this URL with your client. Sometimes, the token requires a scope to be defined. If that’s the case, you might need to add "scope": "your_scope" to your payload.

    Please check any error messages you receive. They often provide clues about what’s going wrong.

    You don’t necessarily need any Azure Tool or APIM setup on your local machine to call this API. Once you have the token, you should be able to make requests to the API from anywhere, including your Jupyter notebook.

    do let me know incase of further queries, I would be happy to assist you.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.