用于将电子邮件标记为已读的Python MSAL PATCH CompactToken解析失败

tyky79it  于 2022-11-21  发布在  Python
关注(0)|答案(1)|浏览(172)

我有一个程序,利用MS Graph API和拉昨天收到的电子邮件,有一个附件,还没有读。我的端点看起来像这样:

'https://graph.microsoft.com/v1.0/me/mailFolders/inbox/messages?$expand=attachments&$search="hasAttachments:true AND received:yesterday AND isRead:false"'

在我进行身份验证并提取所需的数据之后,我将内容字节发送到一个s3 bucket,并存储在下面的块中:

result = app.acquire_token_silent(config["scope"], account=a)
s3     = boto3.client('s3')
bucket = config['bucket']
for email in emails:
    if email['hasAttachments']:
        attachments = email['attachments']
        count = 0
        for attachment in attachments:
            if attachment['isInline'] is False:
                name = attachment['name']
                fileContent =  base64.b64decode(attachment['contentBytes'])
                s3.put_object(Bucket=bucket, Key=name, Body=fileContent)
                count = count +1
                emailid = email['id']
                rp      = requests.patch(f'https://graph.microsoft.com/v1.0/me/messages/{emailid}', data ={'isRead':'true'}, headers = {"Authorization": f"Bearer token= {result['access_token']}"})
                print(rp)
                logger.info('Attachment uploaded-' + ' Attachment Size: ' + str(attachment['size']) + ' File Name: ' + attachment['name'] + ' Email Source: ' + email['from']['emailAddress']['address'] + ' Email ID: ' + email['id'] + ' Email Subject Line: ' + email['subject'])     
        logger.info(f"Attachment count uploaded to s3: {count}")
logger.info('All uploads complete')

一切正常,除了我似乎无法标记电子邮件为已读,所以它不会再次与另一个请求拉。
这是我得到的完整错误:

'{"error":{"code":"InvalidAuthenticationToken","message":"CompactToken parsing failed with error code: 80049217","innerError":{"date":"2022-11-17T18:48:54","request-id":"<xxx>","client-request-id":"<xxx>"}}}'

我确定我的格式有问题,或者可能是我指定的范围有问题。我目前新增的唯一范围是[“Mail.ReadWrite”]。
我是否只是格式不正确,或者是否有其他原因触发了该错误

vuktfyat

vuktfyat1#

它应该是:

requests.patch(f'https://graph.microsoft.com/v1.0/me/messages/{emailid}', json={'isRead': True}, headers = {"Authorization": f"Bearer token= {result['access_token']}"})

相关问题