microsoftgraph / msgraph-sdk-python

MIT License
375 stars 55 forks source link

[Error] sites get_by_path_with_path is not working with invalid request. #279

Open HakjunMIN opened 1 year ago

HakjunMIN commented 1 year ago

Client id and secret was made with having API permission as below.

Files.Read.All
Files.ReadWrite.All
Sites.FullControl.All

What I want to download file of URL is https://mngenvmcap115298.sharepoint.com/sites/sampleteam/Shared%20Documents/general/PDF-ER.docx

async def getfile():
    file = await client.sites.by_site_id("0aa18ca9-16b0-4673-abf0-b14ec9649ee4").get_by_path_with_path("/Shared%20Documents/General/PDF-ER.docx").get()
    return file

value = asyncio.run(getfile())

error is here

{"message": null, "response_status_code": 400, "response_headers": "Headers({'cache-control': 'no-store, no-cache', 'transfer-encoding': 'chunked', 'content-type': 'application/json', 'content-encoding': 'gzip', 'vary': 'Accept-Encoding', 'strict-transport-security': 'max-age=31536000', 'request-id': 'ec72f171-946a-4628-8d06-1cc51cd368ce', 'client-request-id': '6b0d9456-23c7-4633-af1a-91cfca73d637', 'x-ms-ags-diagnostic': '{\"ServerInfo\":{\"DataCenter\":\"Japan East\",\"Slice\":\"E\",\"Ring\":\"5\",\"ScaleUnit\":\"000\",\"RoleInstance\":\"TY1PEPF0000A88F\"}}', 'date': 'Tue, 01 Aug 2023 08:38:04 GMT'})", "additional_data": {}, "error": {"additional_data": {}, "code": "invalidRequest", "details": null, "inner_error": {"additional_data": {"date": "8:38:05"}, "client_request_id": "6b0d9456-23c7-4633-af1a-91cfca73d637", "date": null, "odata_type": null, "request_id": "ec72f171-946a-4628-8d06-1cc51cd368ce"}, "message": "Invalid request", "target": null}}

How can I resolve?

Using msgraph-core 1.0.0a4, msgraph-sdk 1.0.0a13 which might be latest since I installl it today. WSL2, Python 3.10.12

paulschmeida commented 10 months ago

I can confirm that this is a bug in the way url for this call is being built. Here's an example python code:

await (
    client.sites.by_site_id("root")
    .get_by_path_with_path("/sites/MY_SITE_NAME")
    .get()
)

which executes this call -https://graph.microsoft.com/v1.0/sites/root/getByPath(path='%2Fsites%2FMY_SITE_NAME') and doesn't work.

Based on Graph explorer, this should map to a request looking like this instead - https://graph.microsoft.com/v1.0/sites/root:/sites/MY_SITE_NAME

I'm not trying to open a pull request for this as this repo is generated, but accessing sharepoint sites by name is a very basic functionality which was present in legacy sharepoint api so I'm hoping this could get fixed fast.

andrueastman commented 3 months ago

To make the request https://graph.microsoft.com/v1.0/sites/root:/sites/MY_SITE_NAME

You can do this

client.sites.by_site_id("root:/sites/MY_SITE_NAME")
    .get()

The getByPath requestbuilder should probably be removed from the metadata as this path is invalid.