pythonsharepointoffice365office365-rest-client

I can't get the folders from my sharepoint site, how can i resolve this?


I'm currently using a python program to convert documents to store in a databse. For this I need to access sharepoint sites and download the neccesary files.

I've been using multiple tutorials and examples dealing with the office365 REST py library but whatever I try I can't even seem to be able to see the folders that i want to work with.

The code I am currently running

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.runtime.client_request_exception import ClientRequestException


site_url = "https://myorg.sharepoint.com/sites/<my_site_name>"



ctx_auth = AuthenticationContext(site_url)
ctx_auth.acquire_token_for_user("user","pass")   
ctx = ClientContext(site_url, ctx_auth)


folder_rel_url = "/sites/<my_site_name>/Data/<myFolder>"
folder = ctx.web.get_folder_by_server_relative_url(folder_rel_url).select("Exists").get().execute_query()
if folder.exists:
    print("Folder is found")
else:
    print("Folder not found")


def try_get_folder(url):
    try:
        return ctx.web.get_folder_by_server_relative_url(url).get().execute_query()
    except ClientRequestException as e:
        if e.response.status_code == 404:
            return None
        else:
            raise ValueError(e.response.text)

The homepage link of the sharepoint site = "https://mysite.sharepoint.com/sites/<my_site_name>"

site makeup

I've tried multiple solutions including: Python - Download files from SharePoint site This solution simply gave me an empty return {} when changing the variables exactly as told in the tutorial

I've also tried changing the urls: /sites/<my_site_name>/Shared%20Documents/ /sites/<my_site_name>/Start/Data etc


Solution

  • Since the technology I specialize in is the Microsoft Graph API, which also encapsulates endpoints for accessing SharePoint sites: GET https://graph.microsoft.com/v1.0/sites/{site-id} and downloading files: GET /sites/{siteId}/drive/items/{item-id}/content, I would recommend using the Microsoft Graph API.

    To use the graph Api, you first need to authenticate and acquire a token, which contains the permissions required to use a specific endpoint, and you need to register your app in AD partial and add permissions to it.

    You can refer to the official documentation to follow the complete guidance.

    Hope this helps. If you need any more help, please feel free to comment below. Best Wishes.