[Python] MS Graph request returns 403

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    To automate processing of my e-mail messages on MS account, I have registered some application called "NameOfMyRegisteredApplication"; gave it all MS Graph Read rights; and then I ran the following test() under Ubuntu 24.10 with Python 3.12:

    def test():
    #these 3 strings are provided by MS during registration of my application:
    sClntId = '????????-????-????-????-????????????' # Application (client) ID
    sClntCrd = '??????????????????????????????????' # client secret / password
    sObjId = '????????-????-????-????-????????????' # Directory (tenant) ID
    oCCA = ConfidentialClientApplication(client_id=sClntId,client_credential=sClntCrd,
    authority=F'https://login.microsoftonline.com/{sObjId}')
    oScps = ['https://graph.microsoft.com/.default'] # scope(s) for MS Graph
    oRslt = oCCA.acquire_token_for_client(oScps)
    strTkn = oRslt[ 'access_token' ]
    ''' the resulting strTkn has the following fields:
    {
    "aud":"https://graph.microsoft.com",
    "iss":"https://sts.windows.net/sObjId/",//see above
    "iat":t,//[sec]
    "nbf":t,
    "exp":t+3900,
    "aio":"????????????????????",
    "app_displayname":"NameOfMyRegisteredApplication"
    "appid":"sClntId",
    "appidacr":"1",
    "idp":"https://sts.windows.net/sObjId/",
    "idtyp":"app",
    "oid":"????????-????-????-????-????????????",
    "rh":"??????????????????????????????????????????",
    "sub":"????????-????-????-????-????????????",
    "tenant_region_scope":"NA",
    "tid":"sObjId",
    "uti":"????????????????????????????
    "ver":"1.0",
    "wids":["????????-????-????-????-????????????"],
    "xms_ftd":"????????????????????",
    "xms_idrel":"i jk",
    "xms_rd":"?????????????????????????????",
    "xms_tcdt":dddddddddd
    }
    '''
    sGrfUrl = 'https://graph.microsoft.com/v1.0'
    sUserId = '????????-????-????-????-????????????' #this is my e-mail box id
    sUrl = F'{sGrfUrl}/users/{sUserId}/mailFolders/Inbox/Messages'
    sResp = requests.get(sUrl,
    headers={'Authorization':F'Bearer {strTkn}',
    'Content-Type':'application/json'})
    print(sResp) # <Response [403]>
    return;


    Please let me know what is missing in the above script. Thanks in advance.

    Continue reading...

Compartilhe esta Página