如何通过用户名和密码传递到sharepoint?

2024-10-01 07:51:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用sharepylogging连接sharepoint。我用下面的代码连接

import sharepy
import logging


SPUrl = "https://vvv.sharepoint.com"

username = "testuserb@vvvv.onmicrosoft.com"
password = "aaa@123"
s = sharepy.connect(SPUrl,username,password)
s.save()

headers = {"accept": "application/json;odata=verbose",
"content-type": "application/x-www-urlencoded; charset=UTF-8"}
fileToUpload = "copyUpload.py"

with open(fileToUpload, 'rb') as read_file:
    content = read_file.read()
p = s.post("https://aaa.sharepoint.com/sites/vvv/_api/web/getfolderbyserverrelativeurl('/sites/aaa/bbb/')/Files/add(url='"+fileToUpload+"',overwrite=true)", data=content, headers=headers)

print(fileToUpload+" Uploaded in SP")
os.remove(fileToUpload)
logging.info("Uploaded file: with response file")

当我将值传递到connect函数时,它抛出以下错误

AttributeError: 'SharePointSession' object has no attribute 'cookie'

假设,如果我没有把这个值作为参数在终端中传递,那么在终端上输入它之后,它会要求输入用户名和密码,这是正常的。在

但我怎样才能解决问题呢?在

我面临以下错误

^{pr2}$

Tags: httpsimportcomreadloggingusernamecontentfile
2条回答

如果验证成功,库sharepy将存储cookie。你可以用

if not hasattr(s, 'cookie'):
    print("authentication failed!"); quit()

如果验证失败,则在保存SharePoint会话之前。在

常见错误

  • 用户名错误(请检查用户名中的域名是否与站点匹配)
  • 密码错误
  • 错误的站点url(没有后面有其他字符sharepoint.com,特别是没有“/”)

我会检查一下页面的认证要求。在我看来,它正在寻找一个用于身份验证的cookie。通常应用程序需要一个cookie来在您进行身份验证后发布数据。在将POST请求发送到服务器之前,请尝试打印出cookie。如果它打印出一些内容,请尝试将cookie与POST请求一起发送,方法是包含一个cookie参数。在

相关问题 更多 >