【问题标题】:Azure: The access token has been obtained from wrong audience or resourceAzure:已从错误的受众或资源获取访问令牌
【发布时间】:2016-03-26 20:41:34
【问题描述】:

尝试创建一个简单的任务来列出 Azure 门户中的所有资源。我按照给定 URL 中的指示成功收到了令牌。

http://azure-sdk-for-python.readthedocs.org/en/latest/resourcemanagement.html#authentication

但是使用令牌和 superscription_id 的组合,我收到以下错误。

错误:

azure.common.AzureHttpError: {"error"{"code":"AuthenticationFailed","message":"The access token has been obtained from wrong audience or resource '00000002-0000-0000-c000-000000000000'. It should exactly match (including forward slash) with one of the allowed audiences 'https://management.core.windows.net/','https://management.azure.com/'."}}  

我在 Active Directory 中创建了一个应用程序并将所有权限分配给 Windows Active Directory

以下是获取token的代码:

def get_token_from_client_credentials(endpoint, client_id, client_secret):
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
        # 'resource': 'https://management.core.windows.net/',
    }
    response = requests.post(endpoint, data=payload).json()
    return response['access_token']

auth_token = get_token_from_client_credentials(endpoint='https://login.microsoftonline.com/11111111111-1111-11111-1111-111111111111/oauth2/token',
             client_id='22222222-2222-2222-2222-222222222222',
             client_secret='test/one/year/secret/key',

尝试在以下代码中使用此令牌:

def get_list_resource_groups(access_token, subscription_id):
    cred = SubscriptionCloudCredentials(subscription_id, access_token)
    resource_client = ResourceManagementClient(cred)
    resource_group_list = resource_client.resource_groups.list(None)
    rglist = resource_group_list.resource_groups
    return rglist

【问题讨论】:

    标签: python azure azure-resource-manager


    【解决方案1】:

    这不影响(它是一个可选参数)

    实际上,Service to Service Calls Using Client Credentials 流中需要resource 参数来获取访问令牌,该参数告诉您的应用程序从哪里获取令牌。由于需要对 ARM 请求进行身份验证,因此需要在 get_token_from_client_credentials() 中设置 'resource': 'https://management.core.windows.net/'

    我们还可以从您的错误消息中获取信息:

    已从错误的受众或资源“00000002-0000-0000-c000-000000000000”获取访问令牌。它应该与允许的受众“https://management.core.windows.net/”、“https://management.azure.com/”之一完全匹配(包括正斜杠)

    如有任何疑问,请随时告诉我。

    【讨论】:

      【解决方案2】:

      看起来您已将这一行注释掉了?

      'resource': 'https://management.core.windows.net/',
      

      这是您为其获取令牌的受众,因此您将需要此行。

      【讨论】:

      • 这不影响(它是一个可选参数)
      猜你喜欢
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 2018-02-06
      • 2014-05-08
      • 2021-08-03
      • 2022-01-05
      • 1970-01-01
      • 2018-06-18
      相关资源
      最近更新 更多