1

I am trying to make REST Call for azure using python, Have created Access token using ADAL in python. But getting Error called "provided Authorization header is in invalid format." Here is the code for that:

    import adal
    import requests

    token_response = adal.acquire_token_with_username_password(
    'https://login.windows.net/abcd.onmicrosoft.com',
    'user-name',
    'password'
    )

    access_token = token_response.get('accessToken')

    url = 'https://management.azure.com/subscriptions/{subscription-    id}/providers/Microsoft.Network/virtualnetworks?api-version=2015-06-15'
    headers = {'Content-Type': 'application/json',
          'Authorization': access_token}

    response = requests.get(url=url,headers = headers)
    print(response.status_code)
    print(response.text)

Can anyone tell me how the access-token should look like? And is this the correct way to generate token for REST in python? I am reffering this link for above code: https://msdn.microsoft.com/en-us/library/azure/mt163557.aspx

bhavini dave
  • 39
  • 1
  • 6

2 Answers2

1

As @GauravMantri said, the format of the value of the header Authorization is Bearer <access-token> that you can refer to the section Calling ARM REST APIs of the doc "Resource Manager REST APIs".

For example in the section above.

GET /subscriptions/SUBSCRIPTION_ID/resourcegroups?api-version=2015-01-01 HTTP/1.1
Host: management.azure.com
Authorization: Bearer YOUR_ACCESS_TOKEN
Content-Type: application/json

Peter Pan
  • 23,476
  • 4
  • 25
  • 43
0

You would need to prepend Bearer to your token. Something like:

headers = {'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + access_token}
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241