question

AndreNeivaLiboreiro-8620 avatar image
0 Votes"
AndreNeivaLiboreiro-8620 asked AmitSingh-3145 edited

How to connect Python program to Sharepoint REST API using client id and secret provided

Hi folks,
I need some orientation on how to proceed to connect my Python program to a Sharepoint REST API.
The following information was already provided to me:
I was thinking on requests and requests_ntml2 to do that but I dint see any information about the connection and how to set the parameters there.

Client Id:
Client Secret:  
Title:  
App Domain:  
Redirect URI:  

Would appreciate if you code send a code snipet.

THANKS

Andre

office-sharepoint-online
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @AndreNeivaLiboreiro-8620 ,

Have you tried the Office365-REST-Python-Client 2.1.7 ? If it's helpful to this question, you could accept as answer so that it could also help others in the forum :)

Thanks
Best Regards

1 Vote 1 ·

Thanks @Jerryzy-MSFT but I already tried this package and could not see any way of using its parameters with client id and secret. It gives me a token access error.

0 Votes 0 ·
Jerryzy avatar image Jerryzy AndrNeivaLiboreiro-9777 ·

Hi @AndrNeivaLiboreiro-9777,

I have provide a code sample for your reference. Please check it.
And I suggest you could switch to the original account @AndreNeivaLiboreiro-8620, so that it will be clear for others in the forum.
If the code demo is helpful to the question, you could also accept as answer so that it could help others in the forum :)

,

0 Votes 0 ·

Hi @Jerryzy !
I tried to execute the code that you have shared , but I am getting the following error :

ctx.execute_query()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/office365/runtime/client_runtime_context.py", line 121, in execute_query
self.pending_request().execute_query()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/office365/runtime/client_request.py", line 91, in execute_query
raise ClientRequestException(*e.args, response=e.response)
office365.runtime.client_request_exception.ClientRequestException: ('-2147024891, System.UnauthorizedAccessException', 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))', '403 Client Error: Forbidden for url: https://adobe.sharepoint.com/sites/<mySiteName>/_api/Web')


I had already done App registration (client ID and secret) from SharePoint using the following url : https://adobe.sharepoint.com/sites/<mySiteName>/_layouts/15/appregnew.aspx
Not sure if I am missing anything.

Please assist.

Thanks
Deesha

0 Votes 0 ·
Jerryzy avatar image
2 Votes"
Jerryzy answered DUTRANNOISAlainHR-0062 commented

Hi @AndreNeivaLiboreiro-8620 ,

Here is a sample to connect SharePoint Online using App Only credential:

 site_url = 'https://contoso.sharepoint.com/'
 app_principal = {
     'client_id': '--client-id-goes-here--',
     'client_secret': '--client-secret-goes-here--',
 }
    
 context_auth = AuthenticationContext(url=site_url)
 context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
    
 ctx = ClientContext(site_url, context_auth)
 web = ctx.web
 ctx.load(web)
 ctx.execute_query()
 print("Web site title: {0}".format(web.properties['Title']))
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Include in code:
from adal import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

Then change this line to:
context_auth = AuthenticationContext(site_url)

BUT I still get this error:
AttributeError: 'AuthenticationContext' object has no attribute 'acquire_token_for_app'

0 Votes 0 ·

Hi @ @Jerryzy ,

I am trying to follow your solutions. Is it possible to get the documentation or references which I can use for my further enhancements? Consider me as beginner.

All your supports will be appreciated.

Best Regards

0 Votes 0 ·

@Ds-8328 and others, you will finder a better example here:
https://stackoverflow.com/questions/53671547/python-download-files-from-sharepoint-site

First, you need an App registration (client ID and secret) from SharePoint or AAD.
This guy highlights the process from the AAD side, but doing it from SharePoint requires a few less steps (google).
https://python.plainenglish.io/all-you-need-to-know-file-handing-in-sharepoint-using-python-df43fde60813

After that, you will need this library: https://pypi.org/project/Office365-REST-Python-Client/

My script started working after I installed the library noted above.

 from office365.runtime.auth.authentication_context import AuthenticationContext
 from office365.sharepoint.client_context import ClientContext
 from office365.sharepoint.files.file import File
    
 app_settings = {
     'url': 'https://comapny.sharepoint.com/sites/spdev2/',
     'client_id': 'aaaa-bbb-473a-a1b2-zzzzzfadfd',
     'client_secret': 'Tteadsfdafdfasdff444gadfd=',
 }
    
 context_auth = AuthenticationContext(url=app_settings['url'])
 context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])
    
 ctx = ClientContext(app_settings['url'], context_auth)
 web = ctx.web
 ctx.load(web)
 ctx.execute_query()
 print("Web site title: {0}".format(web.properties['Title']))

Hope this helps!



2 Votes 2 ·

Need to do the same but with Sharepoint On Premise.
Anybody to share a sample?

0 Votes 0 ·
Jerryzy avatar image
1 Vote"
Jerryzy answered

Hi @AndreNeivaLiboreiro-8620,

You can use Office365-Rest-Python-Client to consume SharePoint Rest API and it supported App-Only Credential authentication (AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)):

Office365-REST-Python-Client 2.1.7



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AmitSingh-3145 avatar image
0 Votes"
AmitSingh-3145 answered AmitSingh-3145 edited

@Jerryzy

The above code gives me access to the website, where by the title is displayed correctly. How would I read a password protected excel file in a folder, let's say, called "General" to make a python dataframe?

Many thanks in advance.

https://company.sharepoint.com/sites/Team/'Shared%20Documents/General/File%20Name%20Here%20v2.xlsm?web=1'

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.