使用Python OneDrive SDK将文件上载到MS SharePoint

使用Python OneDrive SDK将文件上载到MS SharePoint,第1张

使用Python OneDrive SDK将文件上载到MS SharePoint

在sytech的帮助下,我终于找到了一个解决方案。我最初的问题的答案是使用原始的[Python]
oneDrive SDK](https://github.com/OneDrive/OneDrive-sdk-python)
,不是
可能将文件上载到SharePoint的“共享文档”文件夹
Online

site(在撰写本文时):当SDK查询[**资源发现服务**](https://dev.onedrive.com/auth/aad_oauth.htm#步骤-3-发现--onedrive for business resource uri),它会删除
服务api版本不是v2.0。但是,我得到了SharePoint服务使用“v1.0”,所以它被删除了,尽管可以使用API v2.0访问它我也是。
但是,通过扩展“ResourceDiscoveryRequest”类(在
oneDrive SDK),我们可以为此创建一个解决方法。我设法上传了一个文件
这样:

import jsonimport reimport onedrivesdkimport requestsfrom onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest,     ServiceInfo# our domain (not the original)redirect_uri = 'https://example.ourdomain.net/' # our client id (not the original)client_id = "a1234567-1ab2-1234-a123-ab1234abc123"  # our client secret (not the original)client_secret = 'ABCaDEFGbHcd0e1I2fghJijkL3mn4M5NO67P8Qopq+r=' resource = 'https://api.office.com/discovery/'auth_server_url = 'https://login.microsoftonline.com/common/oauth2/authorize'auth_token_url = 'https://login.microsoftonline.com/common/oauth2/token'# our sharepoint URL (not the original)sharepoint_base_url = 'https://{tenant}.sharepoint.com/'# our site URL (not the original)sharepoint_site_url = sharepoint_base_url + 'sites/{site}'file_to_upload = 'C:/test.xlsx'target_filename = 'test.xlsx'class AnyVersionResourceDiscoveryRequest(ResourceDiscoveryRequest):    def get_all_service_info(self, access_token, sharepoint_base_url):        headers = {'Authorization': 'Bearer ' + access_token}        response = json.loads(requests.get(self._discovery_service_url,          headers=headers).text)        service_info_list = [ServiceInfo(x) for x in response['value']]        # Get all services, not just the ones with service_api_version 'v2.0'        # Filter only on service_resource_id        sharepoint_services =  [si for si in service_info_list  if si.service_resource_id == sharepoint_base_url]        return sharepoint_serviceshttp = onedrivesdk.HttpProvider()auth = onedrivesdk.AuthProvider(http_provider=http, client_id=client_id,          auth_server_url=auth_server_url,          auth_token_url=auth_token_url)should_authenticate_via_browser = Falsetry:    # Look for a saved session. If not found, we'll have to    # authenticate by opening the browser.    auth.load_session()    auth.refresh_token()except FileNotFoundError as e:    should_authenticate_via_browser = True    passif should_authenticate_via_browser:    auth_url = auth.get_auth_url(redirect_uri)    pre = ''    while not re.match(r'[a-zA-Z0-9_-]+', pre):        # Ask for the pre        print('Paste this URL into your browser, approve the app's access.')        print('Copy the resulting URL and paste it below.')        print(auth_url)        pre = input('Paste pre here: ')        # Parse pre from URL if necessary        if re.match(r'.*?pre=([a-zA-Z0-9_-]+).*', pre): pre = re.sub(r'.*?pre=([a-zA-Z0-9_-]*).*', r'1', pre)    auth.authenticate(pre, redirect_uri, client_secret, resource=resource)    service_info = AnyVersionResourceDiscoveryRequest().        get_all_service_info(auth.access_token, sharepoint_base_url)[0]    auth.redeem_refresh_token(service_info.service_resource_id)    auth.save_session()client = onedrivesdk.oneDriveClient(sharepoint_site_url + '/_api/v2.0/',   auth, http)# Get the drive ID of the documents folder.documents_drive_id = [x['id']for xin client.drives.get()._prop_listif x['name'] == 'documents'][0]items = client.item(drive=documents_drive_id, id='root')# Upload fileuploaded_file_info = items.children[target_filename].upload(file_to_upload)

Authenticating for a different service gives you a different token.



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5645477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存