为 Microsoft SharePoint 数据导入配置手动令牌刷新身份验证

重要

托管Microsoft SharePoint 连接器处于 Beta 版中。

本页介绍如何为 Microsoft SharePoint 引入 Azure Databricks 配置手动令牌刷新身份验证。

步骤 1:获取 SharePoint 网站 ID

  1. 在浏览器中访问所需的 SharePoint 网站。
  2. /_api/site/id 附加到该 URL。
  3. 键入 Enter

步骤 2:获取 SharePoint 驱动器名称(可选)

如果要在 SharePoint 网站中引入所有驱动器和文档,则可以跳过此步骤。 但是,如果只想导入磁盘驱动器的子集,则需要收集其名称。

可以在左侧菜单中找到驱动器名称。 每个网站都有一个名为 “文档 ”的默认驱动器。 你的单位可能有其他驱动器。 例如,以下屏幕截图中的驱动器包括doclib1subsite1doclib1,等等。

查看 SharePoint 驱动器

某些驱动器可能会被隐藏在列表中。 驱动器创建者可以在驱动器设置中配置此设置。 在这种情况下,“ 网站内容” 部分可能会显示隐藏的驱动器。

查看隐藏的 SharePoint 驱动器

步骤 3:在 Microsoft Entra ID 中创建客户端

此步骤创建可访问 SharePoint 文件的客户端。

  1. 在 Microsoft Azure 门户(portal.azure.com)中,单击 Microsoft Entra ID。 可能需要搜索“Microsoft Entra ID”。

    Azure 门户:Entra ID 卡

  2. 在左侧菜单中的“ 管理 ”部分下,单击 “应用注册”。

  3. 单击“新建注册”。

    Entra ID 应用的新注册按钮

  4. “注册应用程序 ”窗体中:

    • 是否希望其他租户访问此应用程序。

    • 要用于获取身份验证代码的重定向 URL。 请指定以下任一操作:

      • 你自己的服务器的重定向 URL
      • https://127.0.0.1(即使没有正在运行https://127.0.0.1的服务器,应用也会尝试重定向到该页面。代码位于生成的 URL 中,URL 采用以下格式: https://127.0.0.1:5000/oauth2redirect?code=<code>

    注册应用程序表单

    你将被重定向到应用详细信息页。

    OAuth 应用程序详细信息页

  5. 记下以下值:

    • 应用程序(客户端)ID
    • 目录(租户)ID
  6. 单击 “客户端凭据:添加证书或机密”。

  7. 单击“+ 新建客户端密码”。

    + 新建客户端机密按钮

  8. 添加说明。

  9. 单击 添加

    此时会显示更新的客户端机密列表。

  10. 复制客户端机密值并安全地存储它。 离开页面后,无法访问客户端密码。

步骤 4:授予客户端对 SharePoint 文件的访问权限

客户端需要以下两个权限才能访问 SharePoint 文件:

  • sites.read.all
  • offline_access

这允许客户端访问你有权访问的所有站点中的所有文件。 如果对此感到满意,请执行以下步骤:

  1. 在新笔记本中运行以下 Python 代码。 修改tenant_idclient_idredirect_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。

  2. 在浏览器中,访问代码输出中的 URL。

  3. 使用 Microsoft 365 凭据登录。

  4. “请求的权限 ”对话框中,查看请求的权限。 请确认其中包括 sites.read.alloffline_access,并确保您愿意授予它们。

  5. 确认页面顶部的电子邮件 ID 与用于访问 SharePoint 数据的电子邮件匹配。

  6. 确认客户端名称与上一步中的凭据匹配。

    Microsoft权限请求对话框

  7. 单击接受

    你将被重定向到指定的重定向 URL。 还应收到身份验证代码。

  8. 记下身份验证代码。

  9. 如果未收到 所需的权限 提示,请追加 &prompt=consent 到 URL。

步骤 5:获取刷新令牌

此步骤提取刷新令牌以完成授权。

  1. 将以下 Python 代码粘贴到上一步笔记本中的新单元格中。 修改tenant_idclient_idredirect_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)
    
    

后续步骤

  1. 创建连接 以存储已获取的身份验证详细信息。
  2. 创建引入管道