【问题标题】:Azure Active Directory Error. The access token is from the wrong issuerAzure 活动目录错误。访问令牌来自错误的颁发者
【发布时间】:2016-02-22 11:09:21
【问题描述】:

我正在尝试调用 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 的应用程序。

【问题讨论】:

  • 您为“TenantId”传递的值是多少?
  • 我正在传递 OAuth 2.0 授权端点中的字符串,它在 login.windows.net 之后
  • 租户 ID 应该是 GUID 或类似 somevalue.onmicrosoft.com(本质上是您的 Azure AD 名称)的东西。是你提供的吗?
  • @Gaurav,https://manage.windowsazure.com/serco.onmicrosoft.com#Workspaces/ActiveDirectoryExtension/Directory/&lt;Tenant ID GUID&gt;/directoryQuickStart,这就是我要传递的内容。
  • @DSA - 是的,订阅将仅映射到一个活动目录,您必须使用映射到订阅的该目录的“租户 ID”。就我而言,我正在尝试使用未映射到我的订阅的目录。您可以更改设置并将订阅映射到您要使用的目录。

标签: python python-2.7 azure azure-active-directory


【解决方案1】:

正如消息所说,您需要反对login.windows.net 而不是login.microsoftonline.com

token_response = adal.acquire_token_with_client_credentials(
'https://login.windows.net/' + '<tenantId>',
'<ApplicationId>',
'<Application Secret>'

【讨论】:

【解决方案2】:

您的 AD 应用程序似乎有问题。要对 Azure ARM 进行身份验证,您需要一个带有服务主体的 AD。 您可以参考Create Active Directory application and service principal using portalAuthenticating a service principal with Azure Resource Manager 来创建一个新的AD 应用程序。在您的代码中使用这些信息,然后重试。

【讨论】:

  • 您的回答没有解释某些应用程序(例如 Azure PowerShell 和 Visual Studio Cloud Explorer)如何能够在我的目录中没有服务主体的情况下访问 Azure 资源。
【解决方案3】:

这是导致此问题的共同租户和单独租户之间的区别。

您能在另一个帖子Azure Active Directory Authorization "The access token is from the wrong issuer ' 中看到我的答案吗?

希望这会有所帮助。

【讨论】:

    【解决方案4】:

    在客户端凭据中使用

    "https://management.core.windows.net/"
    

    而不是您的代码中的https://login.microsoftonline.com/

    token_response = adal.acquire_token_with_client_credentials( 'https://management.core.windows.net/' + '<tenantId>', '<ApplicationId>', '<Application Secret>'

    我解决了同样的问题。

    谢谢, 布山

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 2020-08-12
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2013-08-01
      • 1970-01-01
      相关资源
      最近更新 更多