简体   繁体   English

Python InvalidHeader error when trying to send http requests to Azure REST API

[英]Python InvalidHeader error when trying to send http requests to Azure REST API

I'm trying to create an http put request to use Azure REST API to create a resource group using parameters that are created with an HTML form. I'm trying to create an http put request to use Azure REST API to create a resource group using parameters that are created with an HTML form. When trying to send the request I receive an error that my header is invalid for the authorization header.尝试发送请求时,我收到一条错误消息,提示我的 header 对于授权 header 无效。

Here is the error I receive这是我收到的错误

Exception has occurred: InvalidHeader
Value for header {Authorization: {'access_token': 'MYACCESSTOKEN', 'token_type': 'Bearer', 'expires_in': 583}} must be of type str or bytes, not <class 'dict'>

Here is my code这是我的代码

@app.route('/storageaccountcreate', methods = ['POST', 'PUT'])
def storageaccountcreate():
    name = request.form['storageaccountname']
    resourcegroup = request.form['resourcegroup']
    subscriptionId = request.form['subscriptionId']
    location = request.form['location']
    sku = request.form['sku']
    headers = {"Authorization": _get_token_from_cache(app_config.SCOPE)}
    url = f'https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourcegroup}/providers/Microsoft.Storage/storageAccounts/{name}?api-version=2019-06-01'
    r = requests.put(url, headers=headers)
    print(r.text)
    return(r.text)

Basically the value of your Authorization token should be in the following format: Bearer <access-token-value> however you're passing the result of your _get_token_from_cache method and hence you're getting this error.基本上,您的Authorization令牌的值应采用以下格式: Bearer <access-token-value>但是您正在传递_get_token_from_cache方法的结果,因此您会收到此错误。

To fix this, please take the access_token and token_type value from this method's result and create Authorization token using the format I specified above.要解决此问题,请从该方法的结果中获取access_tokentoken_type值,并使用我上面指定的格式创建Authorization令牌。 Something like:就像是:

token_information = _get_token_from_cache(app_config.SCOPE)
token_type = token_information['token_type']
access_token = token_information['access_token']
auth_header = token_type + " " + access_token
headers = {"Authorization": auth_header}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure DevOps REST API returning HTTP Status 203 when using Python Requests - Azure DevOps REST API returning HTTP Status 203 when using Python Requests 尝试使用请求库Python发送视频时发生错误 - error when trying to send a video using requests library Python 尝试使用python中的请求访问REST API时出现错误请求(400)错误 - Bad Request (400) error while trying to access REST API using requests in python 尝试使用Python请求将xml文件发送到OpenStreetMap Overpass API - Trying to send xml file using Python Requests to OpenStreetMap Overpass API 尝试将带有urllib2的json数据发送到Toggl API时出现HTTP错误400 - HTTP Error 400 when trying to send json data with urllib2 to toggl api 尝试通过Azure REST API创建VM时出现运行时错误 - Runtime error while trying to create VM through Azure REST API Python - 使用请求调用 REST Api 时出现值错误 - Python - Value Error on REST Api call using requests HTTP 400 错误使用 Python 请求 POST 和对讲 API - HTTP 400 error using Python Requests POST and Intercom API Python 请求 + Marketo REST API - Python Requests + Marketo REST API 尝试使用Tumblr API的带有请求的Python 3,出现错误401? - Python 3 with Requests trying to use Tumblr API, I get error 401?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM