gpt4 book ai didi

python - Microsoft Azure 授权过程中出现错误如何处理?

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:23 26 4
gpt4 key购买 nike

from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session
import requests

client_id = *CLIENT_ID*
client_secret = *CLIENT_SECRET*
auth = HTTPBasicAuth(client_id, client_secret)
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token =oauth.fetch_token(token_url='https://login.microsoftonline.com/*TENANT_ID*/oauth2/token', auth=auth,resource= 'https://management.azure.com/')

data = {'Content-Type':'application/json',
'Authorization': 'Bearer ' + token['access_token']}
r =requests.post('https://management.azure.com/subscriptions/'
'*SUBSCRIPTION_ID*/providers/Microsoft.Compute/'
'locations/eastus/vmSizes?api-version=2016-04-30-preview', headers=data)

我正在尝试获取可用虚拟机的列表,但收到错误:

{'error':{'code':'AuthorizationFailed','message':“对象 ID 为“X”的客户端“X”无权执行操作“Microsoft.Compute/locations/vmSizes/read” '超出范围'/subscriptions/Y'。"}}

enter image description here

最佳答案

首先,您使用的API方法是get而不是post

其次,您需要授予您的 sp Owner 角色,根据错误日志,您没有授予您的服务主体足够的权限,请参阅此 link:assign-application-to-role

enter image description here

我在实验室进行测试,以下代码适用于我。

from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session
import requests

client_id = ''
client_secret = ''
auth = HTTPBasicAuth(client_id, client_secret)
client = BackendApplicationClient(client_id=client_id)
oauth = OAuth2Session(client=client)
token =oauth.fetch_token(token_url='https://login.microsoftonline.com/<tennat id>/oauth2/token', auth=auth,resource= 'https://management.azure.com/')


data = {'Content-Type':'application/json',
'Authorization': 'Bearer ' + token['access_token']}
r =requests.get('https://management.azure.com/subscriptions/<subscription id>/providers/Microsoft.Compute/locations/eastus/vmSizes?api-version=2016-04-30-preview', headers=data)
for i in r:
print i

关于python - Microsoft Azure 授权过程中出现错误如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48627970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com