Scripting HTTP GET request with authorization headers/tokens

I am having issues with an http get request that I can get working using the http bindings but not when using scripting.

Below is a snapshot of the postman URL and headers:

Here's the code that I have. The getToken() functions works properly but the getSPP function gets the error that is also below:

Any help would be appreciated.

Thanks!

You need to be using named parameters here (even if that's not the issue, but try it).

client.get(url=fullUrl, headers={"Ocp-Apim-Subscription-Key": subscriptionKey, "Authorization":bearer})
1 Like

That was it! Thank you, I appreciate the really quick response!

You should probably invalidate this bearer token now that you've posted it publicly.

2 Likes

I'd strongly recommend refactoring your script, for two main reasons:

  1. You shouldn't be instantiating httpClient instances each time you call a project script. They're non-trivial to create, so this is slowing things down and potentially causing resource leaks. Move the call to system.net.httpClient out of the function definitions entirely so you can re-use it.
  2. Take advantage of the fact that httpClient can encode URL parameters and headers for you, instead of manually assembling full URL strings. This makes maintenance much easier:
client = system.net.httpClient()

def getToken():
    url = "https://"
    params = {
        "grant_type": "password",
        "username": "alfonso.lopez@example.com",
        "password": "",
        "response_type": "id_token",
        "scope": "openid+",
        "client_id": ""
    }

    response = client.post(url, params)
    return response.json["access_token"]

def getSPP(token):
    url = "https://example.com"
    params = {
        "deliveryDateFrom": "2024-08-25",
        "deliveryDateTo": "2024-08-26",
        "settlementPoint": "AMO_AMOCO_G1"
    }
    headers = {
        "Ocp-Apim-Subscription-Key": "",
        "Authorization": ("Bearer " + token)
    }

    response = client.get(url, params=params, headers=headers)

    return response.text
1 Like

Thank you for the upgrades and response. I'm sorry but as Kevin mentioned, I did not realize I had my information there such as email and the password. Any chance you could edit that and use fake ones?

Thank you!