使用 Microsoft Graph API 将文件从 Sharepoint 加载到 Databricks

问题描述 投票:0回答:1

我想使用 Microsoft 应用注册和 Microsoft Graph API 从 Sharepoint 加载文件。现在,我收到错误消息,指出我没有获得授权,但我不知道我是否没有必要的权限,或者我是否对要加载的文件的路径做了错误的操作。我拥有 Sites.Read.All 和 Sites.Select 权限。谁能帮助我吗?我的代码如下所示:

sharepoint_site = "company_name.sharepoint.com/sites/site_name"
sharepoint_file_path = "relative_path/file.xlsx"
client_id = dbutils.secrets.get("secret_scope", "client_id")
tenant_id = dbutils.secrets.get("secret_scope", "tenant_id")
client_secret = dbutils.secrets.get("secret_scope", "client_secret")

auth_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
auth_data = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://graph.microsoft.com/.default"
}

auth_response = requests.post(auth_url, data=auth_data)
if auth_response.status_code != 200:
    print("Fehler bei der Authentifizierung")
    print(auth_response.text)
    exit()

access_token = auth_response.json().get("access_token")

headers = {"Authorization": f"Bearer {access_token}"}
download_url = f"https://graph.microsoft.com/v1.0/sites/{sharepoint_site}/drive/root:/{sharepoint_file_path}:/content"
file_response = requests.get(download_url, headers=headers)
if file_response.status_code == 200:
    file_path = "/dbfs/path.xlsx"
    with open(file_path, "wb") as f:
        f.write(file_response.content)
    print("Datei heruntergeladen!")
else:
    print(f"Fehler: {file_response.status_code}")
    print(file_response.text)
api microsoft-graph-api azure-databricks
1个回答
0
投票

使用

Application
权限类型向应用程序授予 Files.Read.All api 权限。

enter image description here

获取访问令牌后,确保您拥有角色, 您可以通过粘贴令牌在 jwt.io 中检查这一点。

enter image description here

接下来,您可以使用以下代码获取站点 ID。

headers = {"Authorization": f"Bearer {access_token}"}

site_res = requests.get("https://graph.microsoft.com/v1.0/sites/root:/sites/<site_name>",headers=headers)
site_id = site_res.json()['id']

然后您使用以下代码请求获取文件。

download_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drive/root:/Identity AR.xlsx:/content"

file_response = requests.get(download_url, headers=headers)
print(file_response.status_code)
if file_response.status_code == 200:
    file_path = "/dbfs/identity.xlsx"
    with open(file_path, "wb") as f:
        f.write(file_response.content)
    print(f"file created")
else:
    print(file_response.status_code)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.