How to fixed 401 unauthorized response.

Anonymous
2023-04-25T11:32:07.0466667+00:00
Hi,

I am trying to access the SharePoint Lists using Access Token. I am currently using Python to generate Access Token and make an API call using the access token in the headers.

I am Getting the Access Token however I am getting 401 unauthorize response when doing API call.

Here's the code:

import adal
import requests
import logging
logging.basicConfig(level=logging.DEBUG)

def get_confer_token():
    tenant_id = "tenantID"
    client_id = "clientID"
    client_secret_value = "SecretValue"
    resource = "https://confertoday.sharepoint.com"

    authority_uri = f"https://login.microsoftonline.com/{tenant_id}"
    context = adal.AuthenticationContext(authority_uri)
    token = context.acquire_token_with_client_credentials(
        resource,
        client_id,
        client_secret_value
    )

    access_token = token['accessToken']
    print(access_token)
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Accept': 'application/json;odata=verbose',
        'Content-Type': 'application/json;odata=verbose'
    }

    # Make API calls using the access token in the headers
    response = requests.get(
        f'{resource}/_api/web/lists/',
        headers=headers
    )

    print("AAAAAAAAA")
    print(response.text)
    print("AAAAAAAAA")
    return access_token


This the is response I am getting upon running, (Getting Unsupported app only token)
and getting 401 unauthorized Error

Unsupported app only token.
Request failed with status code: 401
Microsoft 365 and Office SharePoint For business Windows
Microsoft Security Microsoft Graph
{count} votes

2 answers

Sort by: Most helpful
  1. msft-gu 1,355 Reputation points
    2023-04-25T19:00:14.13+00:00

    Hello @Colleen Villon Good afternoon and thank you for your question. Under the articles below, we need to make sure that we have the necessary permissions: https://learn.microsoft.com/en-us/graph/api/list-get?view=graph-rest-1.0&tabs=http https://learn.microsoft.com/en-us/graph/api/list-list?view=graph-rest-1.0&tabs=http User's image

    Other things that we can do and verify is the following also:

    • if you are using the correct credentials?
    • if you are using the tenantID, clientID and resource URL?
    • if you have regenerated token and use it to access the list?
    • if the credentials you are using has permission to access the site?
    • To test further, can you open an Incognito/InPrivate browser and login then go to the specific site using the credentials.
    • Go to aka.ms/ge (Graph Explorer), sign in and get the list under the sites. I hope I was able to provide an answer to your question. If yes, please upvote my answer and Accept it as an answer.

  2. Rawand Hawez 0 Reputation points
    2025-05-01T11:07:54.82+00:00

    Client secret no longer works, you need to usea Certificate instead, check out this article:

    https://medium.com/@rawandhawez/sharepoint-app-only-auth-when-client-secrets-fail-and-certificates-prevail-ca230b91a601

    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.