azure-devops-python-api icon indicating copy to clipboard operation
azure-devops-python-api copied to clipboard

'DefaultAzureCredential' object has no attribute 'signed_session'

Open msl0 opened this issue 2 years ago • 6 comments

I'm trying to create a python script to interact with Azure DevOps and I have problem with authentication. I don't want to use PAT. This works but I noticed it is recommended to use azure.identity instead of azure.common.credentials.get_azure_cli_credentials().

from azure.common.credentials import get_azure_cli_credentials
from azure.devops.connection import Connection

credential = get_azure_cli_credentials()[0]
connection = Connection(base_url="https://dev.azure.com/org_name", creds=credential)
core_client = connection.clients.get_core_client()
projects = core_client.get_projects()

When I try to use DefaultAzureCredential from azure.identity, I get following error:

'DefaultAzureCredential' object has no attribute 'signed_session'

from azure.identity import DefaultAzureCredential
from azure.devops.connection import Connection

credential = DefaultAzureCredential()
connection = Connection(base_url="https://dev.azure.com/org_name", creds=credential)
core_client = connection.clients.get_core_client()
projects = core_client.get_projects()

azure.identity = 1.12.0 azure.devops = 7.1.0b3 azure.common = 1.1.11

msl0 avatar Jul 25 '23 20:07 msl0

https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/identity/azure-identity/migration_guide.md looks like Microsoft need to update this module to support the azure-identity credentials; until then it won't work

Edit: https://github.com/Azure/azure-sdk-for-python/issues/15330#issuecomment-728163487 this may be a workaround

jackmtpt avatar Sep 29 '23 15:09 jackmtpt

I have prepared a working workaround using the msrest.authentication library. Unfortunately msrest is also deprecated, but it looks like Microsoft still uses it in their projects (az cli).

from azure.identity import DefaultAzureCredential
from msrest.authentication import BasicTokenAuthentication
from azure.devops.connection import Connection

credential = DefaultAzureCredential()
token = credential.get_token('499b84ac-1321-427f-aa17-267ca6975798/.default')
credentials = BasicTokenAuthentication({'access_token': token.token})
organization_url = 'https://dev.azure.com/myorg'
connection = Connection(base_url=organization_url, creds=credentials)

core_client = connection.clients.get_core_client()
get_projects_response = core_client.get_projects()
if get_projects_response:
    for project in get_projects_response:
        print(project.name)

msl0 avatar Mar 04 '24 00:03 msl0

@msl0 workaround worked for me. thanks!

liorbp avatar Apr 30 '24 10:04 liorbp

Bumping

vs4vijay avatar Aug 24 '24 19:08 vs4vijay