microsoft / azure-devops-python-api

Azure DevOps Python API
https://docs.microsoft.com/azure/devops/integrate/index?view=azure-devops
MIT License
593 stars 199 forks source link

'DefaultAzureCredential' object has no attribute 'signed_session' #466

Open msl0 opened 1 year ago

msl0 commented 1 year ago

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

jackmtpt commented 1 year ago

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

msl0 commented 9 months ago

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)
liorbp commented 7 months ago

@msl0 workaround worked for me. thanks!

vs4vijay commented 3 months ago

Bumping

GodVenn commented 3 months ago

Bump

vs4vijay commented 1 month ago

Bumping this. Can we have proper support for Azure Identity (DefaultAzureCredential, etc) on ADO Python API Library?

Just like other SDKs, following should work without any workaround.

credential = DefaultAzureCredential()
organization_url = 'https://dev.azure.com/myorg'
connection = Connection(base_url=organization_url, creds=credentials)

CC: @apudovkin-ms, @nechvatalp, @jasinshaban, @tonybaloney