Office365-REST-Python-Client icon indicating copy to clipboard operation
Office365-REST-Python-Client copied to clipboard

How to authenticate sharepoint on 21vianet

Open arrebnac opened this issue 11 months ago • 1 comments

I tried to authenticate my Sharepoint on 21vianet with client secret, but it reported '401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/web/getFileByServerRelativePath%28DecodedUrl%3D%27https://galab.sharepoint.cn/:x:/g/EdLtDQIxUHxLj0FuemeleMsBaczbg7-bNKcqyDIyHR5utA%3Fe%3DO04gLy%27%29/%5C%24value'

I have identify the authority url and acquire token url by using the url of "https://login.chinacloudapi.cn/{0}".format(test_tenant) and "https://microsoftgraph.chinacloudapi.cn/.default" according to https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/o365-china-endpoints

Please see my screenshot as below

Image

Could you help me where is my code's problem?

arrebnac avatar Jan 20 '25 02:01 arrebnac

Greetings,

assuming file url represents a sharing link, consider to utilize ClientContext instead of GraphClient client and get_file_by_guest_url method to return a file:

ctx = ClientContext(test_site_url).with_access_token(acquire_token)
file = ctx.web.get_file_by_guest_url(file_url).execute_query()

In the acquire_token method, the scope needs to be adjusted accordingly to:

scopes = ["https://galab.sharepoint.cn/.default"]

vgrem avatar Mar 19 '25 20:03 vgrem