There's a workaround for that and you need to start by granting access to Microsoft Graph
which involves Azure AD admin consenting to the required API permissions (example User.Read, Files.Read).
Since mssparkutils
doesn't support REST linked services directly for Managed Identity access, you would need to authenticate using a different method : the Azure Identity library to authenticate to Microsoft Graph:
You may need to install the Azure Identity library if not available in your Synapse Spark environment. This can usually be done via pip install
commands in a notebook cell. However, since package management can vary in managed environments like Synapse, you may need to add the library through the Synapse workspace library management features.
Then use the DefaultAzureCredential
class from the Azure Identity SDK, which supports managed identities among other authentication methods. This class will automatically use the system-assigned managed identity of the Synapse workspace when running in the Azure environment.
And here comes the part for calling Microsoft Graph API. You need to use the Azure Identity library to acquire an access token for Microsoft Graph (using the access token to call the Microsoft Graph API). 3This can be done by setting the Authorization header with the token in HTTP requests to the Graph API endpoints. You can use Python requests
library or any other HTTP client library to make these calls.
Example to better undersrand ;
from azure.identity import DefaultAzureCredential
import requests
# You need to get a token for Microsoft Graph
credential = DefaultAzureCredential()
token = credential.get_token("https://graph.microsoft.com/.default")
headers = {'Authorization': 'Bearer ' + token.token}
# Example: you want to get the profile of the signed-in user
graph_response = requests.get("https://graph.microsoft.com/v1.0/me", headers=headers)
user_profile = graph_response.json()
# You can use the user_profile or other data as needed
print(user_profile)
Please try an d tell us :)