How to set up proper permissions to use Office365-REST-Python-Client to access (upload/delete files) to SharePoint?

cparkinson 25 Reputation points
2023-03-31T14:45:48.9366667+00:00

I set up an app through the SharePoint site and generated a client ID and secret, but when I go to use it through the Office365-REST-Python Client API, I get the following error:

System.UnauthorizedAccessException', 'Access denied.', "403 Client Error: Forbidden for url: {url}

I'm unsure how to get this set up properly. Any help would be appreciated!

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-04-03T01:51:37.23+00:00

    Hi @cparkinson

    You will need to check the steps in grant permission to the app. First register ID and secret in https://contoso.sharepoint.com/sites/sitetitle/_layouts/15/appregnew.aspx

    After generated a client ID and secret, You need to go https://contoso.sharepoint.com/sites/sitetitle/_layouts/15/appinv.aspx grant permission to the app with following xml scope

    <AppPermissionRequests AllowAppOnlyPolicy="true">
      <AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
    </AppPermissionRequests>
    

    You can refer to the following document for more details

    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

    Then you can use the code to access sharepoint

    """
    Example: SharePoint App-Only auth flow
    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
    """
    from examples import sample_site_url, sample_client_id, sample_client_secret
    from office365.runtime.auth.client_credential import ClientCredential
    from office365.sharepoint.client_context import ClientContext
    ctx = ClientContext(sample_site_url).with_credentials(ClientCredential(sample_client_id, sample_client_secret))
    target_web = ctx.web.get().execute_query()
    print(target_web.url)
    
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

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.