0

and thank you for you useful help already.

I am trying to make an API call using python. Sadly, the only documentation of the API is an implementation already existing in C#.

My problem is, that after i acquire an Azure AADTokenCredential object - i simply do not know how to use it in my HTTPS request.

def get_data_from_api(credentials):
        serialNumber = "123456789"
        fromDate = "01/10/2019 00:00:00" # DD/m/YYYY HH:MM:SS
        untilDate = "09/10/2019 00:00:00" # DD/m/YYYY HH:MM:SS

        PARAMS = {
            serialNumber: serialNumber,
            fromDate: fromDate,
            untilDate: untilDate
        }
        url = "https://myapi.azurewebsites.net/api/sensordata/GetBySerialNumber"
        r = requests.get(url = url, header={"Authorization": credentials}, params=PARAMS)
        print(r)
        #data = r.json()
        return data

The credentials is an msrestazure.azure_active_directory.AADTokenCredentials retrieved using the adal package. The above code results in an error as the header object can only be strings. My question is - How do i pass the authorization object in the correct way?

The C# implementation looks like this:

            // Make a request to get the token from AAD
            AuthenticationResult result = Task.Run(async () => await authContext.AcquireTokenAsync(resource, cc)).Result;

            // Get the auth header which includes the token from the result
            string authHeader = result.CreateAuthorizationHeader();

            // ...

            // Prepare a HTTP request for getting data.
            // First create a client
            HttpClient client = new HttpClient();

            // Create the actual request. It is a GET request so pass the arguments in the url is enough
            HttpRequestMessage request = new HttpRequestMessage(
            HttpMethod.Get, $"https://api.azurewebsites.net/api/sensordata/GetBySerialNumber?serialNumber={serialNumber}&from={fromDate}&until={untilDate}");

            // Add the required authorization header that includes the token. Without it the request will fail as unauthorized
            request.Headers.TryAddWithoutValidation("Authorization", authHeader);

            // Prepare the response object
            HttpResponseMessage response = Task.Run(async () => await client.SendAsync(request)).Result;


1 Answer 1

1

So yes! I finally solved it.

My problem was that i was passing on the ADAL object to the requests phase, however what I needed to do was pass on the actual token that is retrieved using: 'credentials = context.acquire_token_with_client_credentials(resource_uri,client_id,client_secret)'.

Here credentials is a dictionary and what the requests needs in the header for authentication was:

header = {
    "Authorization": "Bearer "+credentials["accessToken"]
}

r = requests.get(url=url, headers=header, params=PARAMS)

passing this on to the requests.get method worked!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.