Office365-REST-Python-客户端多重身份验证

问题描述 投票:0回答:1

目标

我想访问公司内的 Sharepoint 文件并将其用作一种 SFTP,但需要重新调整当前的身份验证限制。总而言之,我想在任何 Sharepoint 文件夹中分发和下载最新(已处理)的文件。

问题的背景信息

由于公司的限制,不可能在每台电脑上安装Python,这就是为什么我正在寻找其他选项来通过我的开发创造价值。我们过去也有一堂课可以这样做。 为了实现这一目标,我们使用 Office365-REST-Python-Client 和用户凭据。

现状

几个月以来,我们无法通过 REST 和 Python 访问 Sharepoint。该公司实施了多重身份验证,因此每 X 天我们需要通过手机更新一次访问权限,在手机中我们会显示应用程序中的某些内容,并且需要将其插入手机上的身份验证器中。

我的想法

我的想法是保存从应用程序生成的令牌,并在每次使用我自己开发的基于 Office365-REST-Python-Client 的 Sharepoint 类时加载令牌。但是,我无法通过 Rest 向 Sharepoint 发布请求,因此我可以在手机中输入任何内容并检索令牌,以便我可以保存它。

问题

  1. 我可以创建一个 POST 或任何其他请求,以便我的手机会显示我需要插入一些内容吗?
  2. 对于上述问题,是否可以在没有任何管理员权限的情况下根据用户凭据检索令牌?

我们之前是如何做到的


from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.folders.folder import Folder
from office365.sharepoint.files.file import File

from HELPER import Helper # own created class

import tempfile

class MySharePoint(Helper):
    '''
    This class is the base class for all SharePoint related classes. It contains all the methods that are used by the other classes. 
    One can download, upload and get inormation about files and folders. provide the credentials in the .env file and teh access is granted to the SharePoint site.
    The Sharepoint site name and the document root folder can be changed in the class variables.
    '''
    SHAREPOINT_DOC_ROOT = 'Docs' # The Folder where the files are stored
    SHAREPOINT_SITE = 'TeamSite' # The SharePoint site name comes right after the sites/ in the URL
    def __init__(self, username:Optional[str] = '', password:Optional[str] = '', company_site: Optional[str] = '') -> None:
        self.client_context = {'company_site':company_site, 'username':username, 'password':self.encode_password(password)}
        self.root_folder = None
        self.tempdir = tempfile.TemporaryDirectory(dir=os.path.join('data'), prefix='temp_')
    ...
    @property
    def client_context(self) -> ClientContext:
        return self._client_context
    
    @client_context.setter
    def client_context(self, kwargs:dict) -> None:
        """ This method sets the client context of the SharePoint site. It is used in the other methods to get the files and folders.

        Args:
            kwargs (dict): A dictionary with the following keys: company_site, username, password

        Raises:
            e: An exception that is raised if the authentication fails.
        """        
        try:
            ctx_authorization = AuthenticationContext( os.getenv("SHAREPOINT_SITE", kwargs['company_site']))
            ctx_authorization.acquire_token_for_user( os.getenv("SHAREPOINT_USER", kwargs['username']), os.getenv("SHAREPOINT_PW", self.decode_password(kwargs['password'])))
            
            self._client_context =  ClientContext(os.getenv("SHAREPOINT_SITE", kwargs['company_site']), ctx_authorization)
            logging.info(f"\nSharePoint authentication successful.")
            
        except (ValueError)  as e:
            logging.critical(f"\nSharePoint authentication failed. The following exception has occured:\n{e}\n")
            raise e

>>> # OUTPUT
>>> Due to a configuration change made by your administrator, or because you moved to a new location, you must use multi-factor authentication to access ''

我尝试的一切都与实现的 SharepointClass 密切相关,我也尝试创建客户端 ID 和秘密,但由于我没有任何管理员权限,所以这是不可能的。

python authentication oop office365 sharepoint-online
1个回答
0
投票

干净的解决方案确实是使用客户端 ID / 客户端证书(或秘密),但您的公司需要给您两件事:

  • 您网站集的管理员权限
  • 为站点管理员启用旧版插件权限管理(自夏季以来默认禁用)

在此解决方案之外,不应有任何方法可以规避 MFA 配置,因为它的目的是阻止您执行您想要执行的操作。

我建议像平常一样检查文件在项目/公司中移动的方式,人们会创建团队、共享 OneDrive 文件、创建完整的 SharePoint 网站并使用 OneDrive 在各自的计算机上简单地同步它们,从而使推/拉过程自动进行而且看起来很无形。

© www.soinside.com 2019 - 2024. All rights reserved.