我正在尝试调用Azure ARM Rest API来创建资源组。我正在传递tenant_id,client_id和client_secret来获取访问令牌,该令牌稍后将用作授权头。我的代码如下。应用程序ID是应用程序关闭的客户端ID,应用程序密钥是选择持续时间后生成的密钥。
import adal
import requests
token_response = adal.acquire_token_with_client_credentials(
'https://login.microsoftonline.com/' + '<tenantId>',
'<ApplicationId>',
'<Application Secret>'
)
access_token = token_response.get('accessToken')
endpoint = 'https://management.azure.com/subscriptions/xxxx/resourcegroups/resourcename?api-version=2015-01-01'
headers = {"Authorization": 'Bearer ' + access_token}
json_output = requests.put(endpoint,headers=headers).json()
print json_output
但这会给我一个错误,如下所示
{u'error': {u'message': u"The access token is from the wrong issuer 'https://sts
.windows.net/xxx/'. It must match the tenant 'h
ttps://sts.windows.net/xxx/' associated with th
is subscription. Please use the authority (URL) 'https://login.windows.net/xxx' to get the token. Note, if the subscription is
transferred to another tenant there is no impact to the services, but informatio
n about new tenant could take time to propagate (up to an hour). If you just tra
nsferred your subscription and see this error message, please try back later.",
u'code': u'InvalidAuthenticationTokenTenant'}}
这个错误意味着什么,我是否传递了正确的凭据。如果我使用错误中提到的凭据,我会收到另一个错误,指出找不到提到的client_id的应用程序。
答案 0 :(得分:0)
正如消息所示,您需要反对login.windows.net
而不是login.microsoftonline.com
:
token_response = adal.acquire_token_with_client_credentials(
'https://login.windows.net/' + '<tenantId>',
'<ApplicationId>',
'<Application Secret>'
答案 1 :(得分:0)
您的AD应用程序似乎存在问题。要对Azure ARM进行身份验证,您需要具有服务主体的AD。 您可以参考Create Active Directory application and service principal using portal或Authenticating a service principal with Azure Resource Manager来创建新的AD应用程序。在代码中使用这些信息,然后重试。
答案 2 :(得分:0)
共同租户和独立租户之间的区别导致了这个问题。
请您在另一个帖子Azure Active Directory Authorization "The access token is from the wrong issuer '中查看我的答案吗?
希望这会有所帮助。
答案 3 :(得分:0)
在客户端凭据中使用
tryAdvance
而不是代码中的"https://management.core.windows.net/"
。
https://login.microsoftonline.com/
我修复了同样的问题。
谢谢, 普山