Need help with Microsoft Graph Client

Anonymous
2023-08-26T14:16:32.6466667+00:00

i am trying to access email from outlook with the subject line Verififcation Email , below is the error and my code please assist on the same

from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient
import warnings
import asyncio

from msgraph.generated.admin.service_announcement.messages.messages_request_builder import MessagesRequestBuilder

# Filter out the NotOpenSSLWarning
warnings.simplefilter("ignore", category=UserWarning)

async def main():
    credential = ClientSecretCredential(
        'Tenant_ID',
        'Client_ID',
        'Client_Secret',
    )

    scopes = ['User.Read', 'Mail.ReadWrite', 'Mail.Read']
    client = GraphServiceClient(credential, scopes=scopes)


    user_id_or_upn = "******@test.com"  # Replace with the user's ID or UPN
    messages_request = client.users.by_user_id(user_id_or_upn).mail_folders.by_mail_folder_id('inbox').messages


    query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
        filter="subject eq 'Verification Email'"
    )
    request_config = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
        query_parameters=query_params
    )
    messages = await messages_request.get(request_config)

loop = asyncio.get_event_loop()

loop.run_until_complete(main())
Content: {"error":"invalid_scope","error_description":"AADSTS1002012: The provided value for scope User.Read is not valid. Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).\r\nTrace ID: 2f3cdbf3-1c31-46c8-b2c7-555164872f00\r\nCorrelation ID: 9b0dd751-9049-475a-b496-8a7d366bd301\r\nTimestamp: 2023-08-26 13:52:47Z","error_codes":[1002012],"timestamp":"2023-08-26 13:52:47Z","trace_id":"2f3cdbf3-1c31-46c8-b2c7-555164872f00","correlation_id":"9b0dd751-9049-475a-b496-8a7d366bd301"}

Outlook | Windows | Classic Outlook for Windows | For business
Microsoft Security | Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. SokiGuo-MSFT 31,711 Reputation points Microsoft External Staff
    2023-08-28T07:45:33.5933333+00:00

    Hi @Mylapravan, Amith

    Welcome to our forum!

    Please kindly understand that the Outlook tag here we mainly focus on general issues about Outlook desktop client. Looks like the issue is more related to Graph API, which we are not very familiar. The following is for informational purposes only.


    Based on my search, the reasons for the error code AADSTS1002012 could be:

    The provided value for scope {scope} is not valid. Client credential flows must have a scope value with /.default suffixed to the resource identifier (application ID URI).

    User's image

    Also, I found a similar thread, you can refer to: https://stackoverflow.com/questions/73210134/microsoft-graph-authentication-problem-when-switching-to-client-secret-the-scop.

    Hope this helps!


    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 comments No comments

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.