Erro 401 "generalException" ao criar item em lista do SharePoint via Microsoft Graph API

Bruno Gomes da Silva 30 Reputation points
2025-05-28T17:34:39.27+00:00

Erro 401 "generalException" ao criar item em lista do SharePoint via Microsoft Graph API

Descrição: Estou tentando criar um item em uma lista do SharePoint Online usando a Microsoft Graph API, mas estou recebendo o seguinte erro:

Microsoft Security | Microsoft Entra | Microsoft Entra ID

Answer accepted by question author
Sridevi Machavarapu 5,885 Reputation points Volunteer Moderator
2025-05-31T06:56:24.3233333+00:00

Hello Bruno Gomes da Silva,

I understand you are trying to create an item in a SharePoint list using Microsoft Graph with app-only authentication and getting a 401 generalException error. This usually happens when the app doesn't have the correct permissions, or those permissions haven't been approved by an admin.

Initially, I too got same error when I ran your code without granting Application type permission:

User's image

Since you're using the client credentials flow, your app needs Application type permission like Sites.ReadWrite.All with admin consent like this:

User's image

After that, run below sample python code that first creates a new item in the list and then lists all items.

import requests
from msal import ConfidentialClientApplication

CLIENT_ID = "appId"
CLIENT_SECRET = "secret"
TENANT_ID = "tenantId"

SHAREPOINT_SITE_ID = "siteId"
LIST_ID = "listId"

AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
SCOPE = ["https://graph.microsoft.com/.default"]

app = ConfidentialClientApplication(
    client_id=CLIENT_ID,
    authority=AUTHORITY,
    client_credential=CLIENT_SECRET
)

token_result = app.acquire_token_for_client(scopes=SCOPE)

if "access_token" not in token_result:
    print("Authentication failed:")
    print(token_result)
    exit(1)

access_token = token_result["access_token"]

headers = {
    "Authorization": f"Bearer {access_token}",
    "Accept": "application/json",
    "Content-Type": "application/json"
}

# Create new list item 
create_url = f"https://graph.microsoft.com/v1.0/sites/{SHAREPOINT_SITE_ID}/lists/{LIST_ID}/items"
payload = {
    "fields": {
        "Title": "Item created with Graph API"
    }
}

create_response = requests.post(create_url, headers=headers, json=payload)
print(f"Create Status: {create_response.status_code}")
print("Create Response:", create_response.text)

# List all fields of all items
list_url = f"https://graph.microsoft.com/v1.0/sites/{SHAREPOINT_SITE_ID}/lists/{LIST_ID}/items?expand=fields"
list_response = requests.get(list_url, headers=headers)

print(f"\nList Status: {list_response.status_code}")

if list_response.status_code == 200:
    items = list_response.json().get("value", [])
    print("Items found:", len(items))
    for item in items:
        print(f"Item ID: {item['id']}")
        for key, value in item.get("fields", {}).items():
            print(f"  {key}: {value}")
        print("------")
else:
    print("List Response:", list_response.text)

User's image

To confirm that, I checked the same in SharePoint site where item created successfully like this:

User's image

Let me know if you still need help with setup or testing. Happy to assist.

Hope this helps!


If this answers your query, do click Accept Answer and Yes for was this answer helpful, which may help members with similar questions.

User's image

If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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