重要
托管Microsoft SharePoint 连接器处于 Beta 版中。
本页介绍如何为 Microsoft SharePoint 引入 Azure Databricks 配置手动令牌刷新身份验证。
步骤 1:获取 SharePoint 网站 ID
- 在浏览器中访问所需的 SharePoint 网站。
- 将
/_api/site/id附加到该 URL。 - 键入 Enter。
步骤 2:获取 SharePoint 驱动器名称(可选)
如果要在 SharePoint 网站中引入所有驱动器和文档,则可以跳过此步骤。 但是,如果只想导入磁盘驱动器的子集,则需要收集其名称。
可以在左侧菜单中找到驱动器名称。 每个网站都有一个名为 “文档 ”的默认驱动器。 你的单位可能有其他驱动器。 例如,以下屏幕截图中的驱动器包括doclib1subsite1doclib1,等等。
某些驱动器可能会被隐藏在列表中。 驱动器创建者可以在驱动器设置中配置此设置。 在这种情况下,“ 网站内容” 部分可能会显示隐藏的驱动器。
步骤 3:在 Microsoft Entra ID 中创建客户端
此步骤创建可访问 SharePoint 文件的客户端。
在 Microsoft Azure 门户(
portal.azure.com)中,单击 Microsoft Entra ID。 可能需要搜索“Microsoft Entra ID”。
在左侧菜单中的“ 管理 ”部分下,单击 “应用注册”。
单击“新建注册”。
在 “注册应用程序 ”窗体中:
是否希望其他租户访问此应用程序。
要用于获取身份验证代码的重定向 URL。 请指定以下任一操作:
- 你自己的服务器的重定向 URL
-
https://127.0.0.1(即使没有正在运行https://127.0.0.1的服务器,应用也会尝试重定向到该页面。代码位于生成的 URL 中,URL 采用以下格式:https://127.0.0.1:5000/oauth2redirect?code=<code>
你将被重定向到应用详细信息页。
记下以下值:
- 应用程序(客户端)ID
- 目录(租户)ID
单击 “客户端凭据:添加证书或机密”。
单击“+ 新建客户端密码”。
添加说明。
单击 添加。
此时会显示更新的客户端机密列表。
复制客户端机密值并安全地存储它。 离开页面后,无法访问客户端密码。
步骤 4:授予客户端对 SharePoint 文件的访问权限
客户端需要以下两个权限才能访问 SharePoint 文件:
sites.read.alloffline_access
这允许客户端访问你有权访问的所有站点中的所有文件。 如果对此感到满意,请执行以下步骤:
在新笔记本中运行以下 Python 代码。 修改
tenant_id、client_id和redirect_url。import requests # This is the application (client) id (obtained from the App Registration page) client_id = "" # This is the tenant id (obtained from the App Registration page) tenant_id = "" # A redirect URL is used in OAuth to redirect users to an # application after they grant permission to access their account. # In this setup, the authentication code is sent to this server. redirect_url = "" # ================= Do not edit code below this line. ================== # Authorization endpoint URL authorization_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/authorize" scopes = ["Sites.Read.All"] scope = " ".join(["https://graph.microsoft.com/{}".format(s) for s in scopes]) scope += ("&offline_access") # Construct the authorization request URL. auth_params = { "client_id": client_id, "redirect_uri": redirect_url, "response_type": "code", "scope": scope } auth_url = authorization_url + "?" + "&".join([f"{key}={value}" for key, value in auth_params.items()]) print(auth_url)输出是 URL。
在浏览器中,访问代码输出中的 URL。
使用 Microsoft 365 凭据登录。
在 “请求的权限 ”对话框中,查看请求的权限。 请确认其中包括
sites.read.all和offline_access,并确保您愿意授予它们。确认页面顶部的电子邮件 ID 与用于访问 SharePoint 数据的电子邮件匹配。
确认客户端名称与上一步中的凭据匹配。
单击接受。
你将被重定向到指定的重定向 URL。 还应收到身份验证代码。
记下身份验证代码。
如果未收到 所需的权限 提示,请追加
&prompt=consent到 URL。
步骤 5:获取刷新令牌
此步骤提取刷新令牌以完成授权。
将以下 Python 代码粘贴到上一步笔记本中的新单元格中。 修改
tenant_id、client_id和redirect_url。# Use the authentication code to get the refresh token. The code contains the characters &session_state=<string> at the end. Remove these characters and define the modified string as code: code = "" # This is the client secret (obtained from the Credentials and Secrets page) client_secret = "" # ================= Do not edit code below this line. ================== token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token" # Request parameters token_params = { "client_id": client_id, "client_secret": client_secret, "code": code, "grant_type": "authorization_code", "redirect_uri": redirect_url, "scope": "profile openid email https://graph.microsoft.com/Sites.Read.All offline_access" } # Send POST request to token endpoint response = requests.post(token_url, data=token_params) # Handle response if response.status_code == 200: token_data = response.json() refresh_token = token_data.get("refresh_token") print("Refresh Token:", refresh_token) else: print("Error:", response.status_code, response.text)