[英]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_token
和token_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.