簡體   English   中英

使用python應用程序將文件上傳到在線共享點時出現錯誤請求

[英]Getting error as Bad request while uploading a file to sharepoint online using python application

我正在嘗試使用 python 應用程序通過 REST API 將文件上傳到 sharepoint o365,但收到錯誤感謝任何幫助在此先感謝請找到下面的腳本

代碼:

url="https://someserver.sharepoint.com/test/test1/_api/web/GetFolderByServerRelativeUrl(@a1)/Files/Add(url=@a2,overwrite = @a3)?@a1=%27%2Ftest%2Ftest1%2FShared%20Documents%27&@a2=%27%25"+temp_file_name+"%27&@a3=false"

req = urllib2.Request(url.encode('utf-8'), data=fragment)
req.get_method = lambda: 'POST'
req.add_header('Authorization', 'Bearer ' + session["access_token"])
req.add_header("accept", "application/json;odata=verbose")
req.add_header('X-Target-URI', 'https://someserver.sharepoint.com/_api')
req.add_header('Content-Type', 'application/octet-stream')
req.add_header('Content-length', fsize["filesize"])
req.add_header('x-requestdigest', Digest_value)
opener = OpenerCreator.create()
f = opener.open(req, None, 60)
resJson = f.read()
ret_response = json.loads(resJson)

要與 Microsoft Sharepoint 交互,您需要使用 Microsoft Graph API。

這是它的主頁https://graph.microsoft.io/en-us/docs

從您的驅動器獲取文件https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_downloadcontent

另一種選擇是使用 OneDrive API

進口請求

def handle_file_upload(file_to_upload):

    """ Function to upload files to our sharepoint directory """

    # Getting the authenticated user credentials from python-social-auth

    # This call assumes the user you are trying to access is the logged in user.

    social = request.user.social_auth.get(provider='azuread-oauth2')

    access_token = social.extra_data['access_token']



    # build our header for the api call

    headers = {

        'Authorization' : 'Bearer {0}'.format(access_token),

    }



    # build the url for the api call

    # Look at https://dev.onedrive.com/items/upload_put.htm for reference

    url = settings.SOCIAL_AUTH_AZUREAD_OAUTH2_RESOURCE + '/_api/v2.0/drive/root:/' + file_to_upload.name + ':/content'


    # Make the api call

    response = requests.put(url, data=open(file_to_upload, 'rb'), headers=headers)

    return response

我建議使用O365訪問 Microsoft Sharepoint。 不幸的是,缺少有關 Sharepoint 操作的文檔,但我可以確認它有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM