Sharepoint 文件存储通过密码未过期给出密码过期原因 python

Sharepoint file storage give password expired reason through password not expired using python

无法在 SharePoint 中上传文件,因为昨天代码工作正常,使用了 Sharepoint 中存储的文件,现在出现以下错误:

Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS50055: The password is expired.')

其他两个帐户也发生了同样的事情。 函数uploadMeta_file(filepath_meta)调用Sharepoint

File "c:\Users\Desktop\work\sample\cookiepopup-kana-api.venv\lib\site-packages\shareplum\office365.py", line 80, in get_security_token raise Exception('Error authenticating against Office 365. Error from Office 365:', message[0].text) Exception: ('Error authenticating against Office 365. Error from Office 365:', 'AADSTS50055: The password is expired.')


import requests
import jsonpath
import os, requests, uuid, json
import pandas as pd
from shareplum import Office365
import xlsxwriter
from shareplum import Site
from datetime import datetime 
import logging 



def kana_api():
    
    writer.save()
    filepath_meta ="kana1_input.xlsx"
    uploadMeta_file(filepath_meta)
    


    #metadata file
def uploadMeta_file(filepath_meta): 


# working with crentials
    username = "myid
    password = "mypassword"
    site_name = "AFFinance"
    base_path = "validpath"
    doc_library ="validlib"
        
    
    file_name = filepath_meta
    # Obtain auth cookie
    authcookie = Office365(base_path, username=username, password=password).GetCookies()
    session = requests.Session()    
    session.cookies = authcookie
    session.headers.update({'user-agent': 'python_bite/v1'})
    session.headers.update({'accept': 'application/json;odata=verbose'})

    session.headers.update({'X-RequestDigest': "FormDigestValue"})
    response = session.post( url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
                                data="")
    logging.info(response)
    session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
    # perform the actual upload
    with open( file_name, 'rb') as file_input:
        try: 
            response = session.post( 
                url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library  + "')/Files/add(url='" 
                + file_name + "',overwrite=true)",
                data=file_input)
            logging.info("Meta File uploaded")
        except Exception as err: 
            logging.info("Some error occurred....")
    


if __name__== '__main__':
    kana_api()

正如您提到的,您的帐户启用了 MFA。看起来你没有在你的申请中处理同样的事情。

有两种方法可以解决这个问题。

  1. 提供交互式登录 UI 以处理 MFA。

  2. 创建一个禁用了 MFA 的服务帐户。从而使用该账号访问Sharepoint Online,就不会出现上述问题。

  3. 仅使用应用程序身份验证

什么是 Sharepoint 应用程序?

仅应用程序身份验证 - 不需要用户凭据,但您可以通过对应用程序进行身份验证来获取数据。此外,应用程序身份验证将间接绕过 MFA(仅应用程序身份验证不受 MFA 影响)。

更多信息:https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

我正在通读 Shareplum Tutorials - 不幸的是,但无法获得仅应用程序身份验证的方法。但你可以在最后仔细检查一下。

如果您的目标是使用 python,您还可以使用以下库:

https://pypi.org/project/Office365-REST-Python-Client/#Working-with-SharePoint-API

要执行仅应用程序身份验证:

client_credentials = ClientCredential('{client_id}'),'{client_secret}')
ctx = ClientContext('{url}').with_credentials(client_credentials)

但是,是的,您将修改代码以执行使用上述库所需的操作。