1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Creating a shortcut in Fabric to connect to ADLS Gen 2 through REST API [closed]

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a shortcut in Fabric. The shortcut will be to an ADLS Gen 2 Parquet folder by calling REST API.

    When I am trying to do the same I am receiving an error on the shortcut REST API. Error:


    HTTPError: 403 Client Error: Forbidden for url: https://api.fabric.microsoft.com/v1/workspaces//items//shortcuts
    ​

    Below is the code.

    import requests
    import sempy.fabric as fabric
    # Get workspace ID
    fabric_workspace_id = fabric.get_workspace_id()
    kvUrl='https://<keyvaultname>.vault.azure.net/'
    secretName_sp_pwd = '<sp secret name>'
    secret_sp_pwd_value = notebookutils.credentials.getSecret(kvUrl,secretName_sp_pwd)

    storageaccountname = '<gen2 account name'
    containername = '<container name'
    # Step 1: Define Azure AD and Fabric credentials
    tenant_id = "<tenantid"
    client_id = "client id"
    client_secret = secret_sp_pwd_value
    scope = "https://api.fabric.microsoft.com/.default"
    token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"

    # Try to create token through ClientSecretCredential
    from azure.identity import ClientSecretCredential

    credential = ClientSecretCredential(tenant_id, client_id, client_secret)
    token = credential.get_token("https://api.fabric.microsoft.com/.default")
    access_token1 = token.token

    print("===========access_token1 check==============")
    import jwt
    decoded = jwt.decode(access_token1, options={"verify_signature": False})
    print("aud:", decoded.get("aud"))
    print("roles:", decoded.get("roles"))
    print("scp:", decoded.get("scp"))
    print(decoded.get("groups"))

    # Step 2: Get access token using Service Principal
    payload = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": scope
    }

    with requests.Session() as session:
    token_response = session.post(token_url, data=payload)
    token_response.raise_for_status()
    access_token = token_response.json().get("access_token")
    print("Access token acquired.")

    print("===========access_token check==============")
    import jwt
    decoded = jwt.decode(access_token, options={"verify_signature": False})
    print("aud:", decoded.get("aud"))
    print("roles:", decoded.get("roles"))
    print("scp:", decoded.get("scp"))
    print(decoded.get("groups"))



    # Step 3: Define shortcut creation parameters
    workspace_id = fabric_workspace_id
    lakehouse_item_id = "<lakehouseId>"
    shortcut_name = "schouseprices"
    shortcut_path = "Tables/sc_houseprices"
    adls_basepath = "https://"+storageaccountname+".dfs.core.windows.net"
    adls_subpath = f"/{containername}/ParquetFiles/houseprice/"
    adls_url = f"{adls_basepath}{adls_subpath}"
    print(f"ADLS URL: {adls_url}")

    shortcut_api_url = f"https://api.fabric.microsoft.com/v1/workspaces/{workspace_id}/items/{lakehouse_item_id}/shortcuts"
    headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json"
    }
    print("========================Headers=======================")
    print(headers)

    shortcut_payload = {
    "path": shortcut_path,
    "name": shortcut_name,
    "target": {
    "adlsGen2": {
    "location": adls_basepath,
    "subpath": adls_subpath,
    "connectionId":"b1d02173-d50b-4f6f-bdb0-31a8197d0ef5"
    }
    }
    }
    print("============Shortcut Payload===============")
    print(shortcut_payload)

    # Step 4: Create the shortcut
    response = session.post(shortcut_api_url, headers=headers, json=shortcut_payload)
    print(response)
    response.raise_for_status()
    print("Shortcut created successfully:", response.json())

    1. The SPN has necessary permissions.
    2. The Fabric Tenant setting is also enabled to be able to call fabric APIs.
    3. The SPN is added as an Admin in the Fabric setings as well.

    Continue reading...

Compartilhe esta Página