how to generate token in synapse notebook using identity

Nitin Khedekar (WIPRO LIMITED) 0 Reputation points Microsoft Vendor
2024-06-24T07:59:14.77+00:00

how to generate token in synapse notebook using identity.
Why below code is not working?

import requests


def get_token_for_risk_api():
    resource = "https://xxxxxx"
    user_managed_client_id = "xxxx"
    api_version = "2019-08-01"
    # Construct the URL for token retrieval
    endpoint = "http://169.254.169.254/metadata/identity/oauth2/token"
    url = f"{endpoint}?api-version={api_version}&resource={resource}&client_id={user_managed_client_id}"
    
    
    # Construct headers (including 'Metadata: true' header)
    headers = {
        'Metadata': 'true'
    }
    
    try:
        # Send GET request to Azure IMDS endpoint
        response = requests.get(url, headers=headers)
        response.raise_for_status()  # Raise an exception for 4xx or 5xx responses
        
        # Parse JSON response and extract access token
        token_response = response.json()
        risk_api_token = token_response['access_token']
        
        return risk_api_token
    
    except requests.exceptions.RequestException as e:
        print(f"Error occurred while obtaining access token: {e}")
        return None

# Example usage:
if __name__ == "__main__":
    token = get_token_for_risk_api()
    if token:
        print(f"Received access token: {token}")
    else:
        print("Failed to obtain access token.")

Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
5,005 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.