Я могу просмотреть и загрузить файл вручную из веб-браузера, используя ссылку ниже. :
https://myfirm.sharepoint.com/sites/mysite/Shared Documents/test.txt
Однако при запуске приведенного ниже кода я получаю сообщение об ошибке ниже. :
Произошло исключение: ClientRequestException
Код: Выделить всё
('-2130575338, Microsoft.SharePoint.SPException', 'The file /sites/mysite/Shared Documents/test.txt does not exist.', "404 Client Error: Not Found for url: https://myfirm.sharepoint.com/sites/mysite/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2Fmysite%2FShared%20Documents%2Ftest.txt')?$select=ServerRelativePath,Id")
https://myfirm .sharepoint.com/sites/mysite/_api/Web/getFileByServerRelativePath(DecodedUrl='%2Fsites%2Fmysite%2FShared%20Documents%2Ftest.txt')
Приведенный ниже код состоит из двух частей, оба взяты из этого github:
Часть 1 ниже работает
https://github.com/vgrem/Office365-REST ... ractive.py
Часть 2 ниже НЕ работает в сочетании с частью 1
https://github.com/vgrem/Office365-REST ... ownload.py
Код: Выделить всё
test_tenant = 'myfirm.onmicrosoft.com'
test_site_url = f'https://{test_tenant}/sites/mysite'
test_client_id = 'xxxx-xxxx-xxxx-xxxx-xxxx'
file_url = "/sites/mysite/Shared Documents/test.txt"
# below is also not working
# file_url = "Shared Documents/test.txt"
ctx = ClientContext(test_site_url).with_interactive(test_tenant, test_client_id) # this works
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
print(download_path)
with open(download_path, "wb") as local_file:
file = (
ctx.web.get_file_by_server_relative_path(file_url)
.download(local_file)
.execute_query() # throws an error here
)
print("[Ok] file has been downloaded into: {0}".format(download_path))
Единый вход включен для Sharepoint, а использование имени пользователя и пароля отключено.< /p>
Регистрация приложения Azure
1 В разделе «Аутентификация» создайте конфигурацию платформы для мобильных и настольных приложений (не веб-приложений). приложения!). Предполагая, что ваш блокнот работает на локальном компьютере, добавьте http://localhost к URI перенаправления.
2 В разделе «Разрешения API» добавьте разрешение от Microsoft Graph API: User.Read
https://poznyakikovsky.medium.com/getti ... 4e8bb723d5
Подробнее здесь: https://stackoverflow.com/questions/790 ... company-fi