使用Microsoft Graph API从SharePoint加载文件至Databricks时的授权错误排查求助
大家好,我现在尝试通过Microsoft应用注册和Microsoft Graph API从SharePoint加载文件到Databricks,但一直遇到授权错误。我不确定是缺少必要权限,还是文件路径设置有问题。目前我的应用已经配置了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)
目前我能成功获取到access_token,但请求文件时返回授权相关错误,实在搞不清是权限配置的问题,还是文件路径的写法不对,麻烦帮忙排查下可能的原因,谢谢!
备注:内容来源于stack exchange,提问作者yannik0103
相关产品推荐
相关产品推荐

