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

Andre Neiva Liboreiro 11 Reputation points
2020-08-26T21:24:59.927+00:00

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

Microsoft 365 and Office SharePoint For business Windows
{count} vote

4 answers

Sort by: Most helpful
  1. ZhengyuGuo 10,586 Reputation points Moderator
    2020-09-03T02:00:34.437+00:00

    Hi @Andre Neiva Liboreiro ,

    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']))  
    
    3 people found this answer helpful.

  2. ZhengyuGuo 10,586 Reputation points Moderator
    2020-08-27T02:24:47.847+00:00

    Hi @Andre Neiva Liboreiro ,

    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

    1 person found this answer helpful.
    0 comments No comments

  3. Amit Singh 1 Reputation point
    2022-02-09T13:36:33.217+00:00

    @ZhengyuGuo

    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'


  4. Rajasekaran, Geetha 0 Reputation points
    2024-04-29T11:38:04.3566667+00:00

    I used the above method - client id and client secret, I keep getting the same error message. ANy thoughts or suggestions on how to fix this...

    ValueError: {"error":"invalid_client","error_description":"AADSTS7000215: Invalid client secret provided. Ensure the secret being sent in the request is the client secret value, not the client secret ID, for a secret added to app <client_id>
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.