You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HP员工使用Python下载SharePoint商业版在线Excel出现403错误求助

问题描述

我是HP公司员工,需要将存储在SharePoint上的商业版在线Excel文件通过Python下载到本地,运行现有代码时接口返回403 FORBIDDEN错误,下载得到的Excel文件内容仅显示403报错,且相同代码在同事设备上可正常运行。
当前使用的文件浏览链接:URL File

原有报错代码

import time
import urllib.request
import pandas as pd
import requests
import json

def share_point_login():
   print('called') 
   site=" url of the excel file"
   username = "username"
   password = "password"
   response = requests.get(site, auth=(username, password))         
   print(response.text)
   print(response.status_code)

share_point_login()

排查与解决方案

  • 认证方式适配问题:HP企业SharePoint默认启用多因素认证(MFA),你当前使用的requests基础HTTP auth不支持MFA校验,若你的账号MFA策略和同事不一致就会出现跨设备可用率差异。建议改用适配SharePoint认证的第三方库Office365-REST-Python-Client实现身份校验,不要直接使用requests的auth参数。
  • 请求地址错误:你当前传入的是SharePoint文件夹浏览页的URL,不是Excel文件的直链,需要先获取目标文件的实际资源地址:进入文件所在页面,右键点击目标Excel文件,选择「复制链接」,确保链接权限设置为「组织内人员可访问」后再调用。
  • 本地环境配置问题:检查你设备的HP企业代理配置,requests默认不会读取系统代理,需要在get请求中显式传入proxies参数,或配置系统环境变量HTTP_PROXY、HTTPS_PROXY为公司代理地址。另外先手动在浏览器访问该文件地址,确认你的账号有该文件的下载权限。
  • 可用代码示例
    首先执行安装命令:
    pip install Office365-REST-Python-Client
    运行代码:
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

# 基础配置
site_url = "https://hp.sharepoint.com/sites/EcosystemQualityTransformation"
username = "你的HP企业邮箱"
password = "你的账号密码"
# 替换为你的Excel文件在SharePoint中的相对路径
file_relative_path = "/sites/EcosystemQualityTransformation/Shared Documents/Integration Testing Workstream/SIV/目标文件名.xlsx"
local_save_path = "./本地保存的文件名.xlsx"

# 认证并下载
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
with open(local_save_path, "wb") as f:
    ctx.web.get_file_by_server_relative_url(file_relative_path).download(f).execute_query()
print("文件下载完成")

内容的提问来源于stack exchange,提问作者akash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:36:03