属性错误:“DefaultAzureCredential”对象没有属性“signed_session”

问题描述 投票:0回答:2

我正在使用 python sdk 使用

azure-applicationinsights==0.1.1
查询应用程序见解。 这是我的代码:

from azure.identity import DefaultAzureCredential
from azure.applicationinsights import ApplicationInsightsDataClient
from azure.applicationinsights.models import QueryBody

def query_app_insights():
    query = 'myEvents | take 10'
    application = 'my-app'
    creds = DefaultAzureCredential()
    client = ApplicationInsightsDataClient(creds)
    result = client.query.execute(application, QueryBody(query=query))

我收到此错误:

AttributeError: 'DefaultAzureCredential' object has no attribute 'signed_session'

也尝试过:

    creds = ClientSecretCredential(tenant_id='something',
                                   client_id='something',
                                   client_secret='something')
    client = ApplicationInsightsDataClient(creds)

它给了我同样的错误。

我也尝试过:

from azure.identity import DefaultAzureCredential
from azure.applicationinsights import ApplicationInsightsDataClient
from azure.applicationinsights.models import QueryBody
from azure.common.credentials import ServicePrincipalCredentials

def query_app_insights():
    query = 'myEvents | take 10'
    application = 'my-app'
    creds = ServicePrincipalCredentials(tenant='something',
                                        client_id='something',
                                        secret='something')
    client = ApplicationInsightsDataClient(creds)
    result = client.query.execute(application, QueryBody(query=query))

它抱怨我的秘密,但它是正确的。 有什么想法吗?

python azure sdk
2个回答
0
投票

AttributeError:“DefaultAzureCredential”对象没有属性“signed_session”:

这个错误最近经常出现,我也遇到过几次。它实际上意味着“没有可供给定租户使用的签名会话”。

完成解决方法后,我发现 SDK 管理库解决了一些问题。

DefaultAzureCredential类已被修改,在少数版本中不再具有‘签名会话’属性。为了解决这个问题,应该升级最新版本的管理库。

需要检查并解决:

  • 在导入

    Python
    代码时安装了最新版本的模块。

     azure-identity 1.12.0
     azure-applicationinsights 0.1.1
    
  • 在 Visual Studio Code 中选择

    python 3.10
    解释器,并在虚拟环境中运行与您相同的代码。

  • 创建了一个虚拟环境并运行了该项目。

      py -m venv env
    .\env\Scripts\activate 
    
  • 有时

    requirements.txt
    文件将无法正确配置,在这种情况下您也可能会遇到此错误。

    为了避免这种情况,请使用终端冻结需求:

    pip freeze requirements.txt

满足上述所有条件后,我在我的环境中进行了尝试,并且能够继续,没有出现任何属性错误。

输出:

enter image description here

或者,我尝试通过在

clientsecretcredential
中注册新应用程序来连接 AzureAD
 

注意:最好创建新的注册,而不是使用旧的注册,因为

client_secrets
可能会过期。

TENANT_ID = '<TENANT_ID>'
CLIENT_ID = '<CLIENT_ID>'
CLIENT_SECRET = '<CLIENT_SECRET>'
SUBSCRIPTION_ID = '<SUBSCRIPTION_ID>'
from  azure.identity  import  ClientSecretCredential
from  azure.mgmt.keyvault  import  KeyVaultManagementClient
credentials = ClientSecretCredential(tenant_id=TENANT_ID, client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
credentialskeyvault = KeyVaultManagementClient(credentials, SUBSCRIPTION_ID)
print ("Logged")

输出:

enter image description here

确定精确状态:

我只是从AzureAD中的应用程序中删除了特定的“

权限/权限
”,并测试了登录时是否会抛出错误。

为什么因为

此错误通常仅在用户登录后抛出。它表明

defaultazurecredential
对象运行没有问题。

enter image description here

如果问题仍然存在,那么您可以通过安装

msrestazure
模块来包装凭据并创建 credentialwrapperclass 详细信息由 @lmazuel。


0
投票

我做了一个包来解决这个问题: https://github.com/kouroshparsa/az_sights

© www.soinside.com 2019 - 2024. All rights reserved.