Ошибка «BadRequest» при использовании запроса SendMail Microsoft Graph API - PullRequest
0 голосов
/ 24 июня 2019

Я пишу скрипт на Python для отправки почты с помощью Microsoft Graph API.Почта отправляется только на те идентификаторы, которые существуют в нашей AAD.

Я написал код:

from adal import AuthenticationContext
import adal
import string
import urllib
import json
import requests
import pprint
import base64
import mimetypes

API_VERSION = 'beta'
RESOURCE = 'https://graph.microsoft.com'
TENANT = <my tenant ID>
CLIENTID = <Client ID>
CLIENTSECRET = <sescret>
DOMAIN = <mydomain>

# AUTHENTICATION - get access token and update session header
def get_session():

    authority_host_url = 'https://login.windows.net'
    tenant = TENANT
    authority_url = authority_host_url + '/' + tenant
    service_endpoint = "https://graph.microsoft.com/"

    client_id = CLIENTID
    client_secret = CLIENTSECRET


    # Retrieves authentication tokens from Azure Active Directory and creates a new AuthenticationContext object.
    context = AuthenticationContext(authority_url, validate_authority=True, cache=None, api_version=None, timeout=300, enable_pii=False)
    # Gets a token for a given resource via client credentials.
    token_response = context.acquire_token_with_client_credentials(service_endpoint, client_id, client_secret)

    # Create a persistent session with the access token
    session = requests.Session()
    session.headers.update({'Authorization': f'Bearer {token_response["accessToken"]}', 'SdkVersion': 'python-adal', 'x-client-SKU': 'DynaAdmin'})
    #session.headers.update({'Authorization': f'Bearer {token_response["accessToken"]}'})

    return session

def build_uri(url):
    if 'https' == urllib.parse.urlparse(url).scheme:
        return url
    result = urllib.parse.urljoin(f'{RESOURCE}/{API_VERSION}/', url.lstrip('/'))

    print("\nURL:\n")
    print(result)

    return result

def sendmail(*, session, subject=None, recipients=None, body='',
             content_type='HTML', attachments=None):
    """Helper to send email from current user.

    session       = user-authenticated session for graph API
    subject      = email subject (required)
    recipients   = list of recipient email addresses (required)
    body         = body of the message
    content_type = content type (default is 'HTML')
    attachments  = list of file attachments (local filenames)

    Returns the response from the POST to the sendmail API.
    """

    # Verify that required arguments have been passed.
    if not all([session, subject, recipients]):
        raise ValueError('sendmail(): required arguments missing')

    # Create recipient list in required format.
    recipient_list = [{'EmailAddress': {'Address': address}}
                      for address in recipients]

    # Create list of attachments in required format.
    attached_files = []
    if attachments:
        for filename in attachments:
            b64_content = base64.b64encode(open(filename, 'rb').read())
            mime_type = mimetypes.guess_type(filename)[0]
            mime_type = mime_type if mime_type else ''
            attached_files.append( \
                {'@odata.type': '#microsoft.graph.fileAttachment',
                 'ContentBytes': b64_content.decode('utf-8'),
                 'ContentType': mime_type,
                 'Name': filename})

    # Create email message in required format.
    email_msg = {'Message': {'Subject': subject,
                             'Body': {'ContentType': content_type, 'Content': body},
                             'ToRecipients': recipient_list,
                             'Attachments': attached_files},
                 'SaveToSentItems': 'true'}

    print("\nBody:\n")              
    print(email_msg)    

    # Do a POST to Graph's sendMail API and return the response.
    resp = session.post(build_uri('/users/8368b7b5-b337ac267220/sendMail'), data=email_msg, stream=True, headers={'Content-Type': 'application/json'})

    return resp

# ------------ RUN CODE ------------

session = get_session()

myRecipients = "sea.chen@mydomain.com;anj.dy@mydomain.com"
response = sendmail(session=session,
                    subject= "hai",
                    recipients=myRecipients.split(';'),
                    body="hai this is a new mail")

print("\nRequest headers: \n")
print(response.request.headers)

print("\nResponse: \n")
print(response) 
print(response.text) 

Вывод, который я получил:

Body:

{'Message': {'Subject': 'hai', 'Body': {'ContentType': 'HTML', 'Content': 'hai this is a new mail'}, 'ToRecipients': [{'EmailAddress': {'Address': 'sea.chen@mydomain.com'}}, {'EmailAddress': {'Address': 'anj.dy@mydomain.com'}}], 'Attachments': []}, 'SaveToSentItems': 'true'}

URL:

https://graph.microsoft.com/beta/users/8368b7b5-b337ac267220/sendMail

Request headers:

{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive', 'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI6IkFRQUJBQUFBQUFEQ29NcGpKWHJ4VHE5Vkc5dGUtN0ZYVzRQOWpybFRZWXdIVmZieWJxd0dUVUtNRThqUG5Va0hFSlowU2Jy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90'}

Response:

<Response [400]>
{
  "error": {
    "code": "BadRequest",
    "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
    "innerError": {
      "request-id": "26ef0c0b-c362-401c-b8ed-48755a45d086",
      "date": "2019-06-24T07:10:53"
    }
  }
}

я получил ошибку:

Невозможно прочитать полезную нагрузку JSON-запроса.Убедитесь, что заголовок Content-Type установлен, а полезная нагрузка имеет допустимый формат JSON.

Поэтому я попытался выполнить запрос в графическом обозревателе и использовал тот же URL-адрес и тело, которые были напечатаны в приведенном выше выводе.Запрос, отправленный из Graph Explorer, был успешным, и письмо было отправлено нужным получателям.Это означает, что content-body является действительным.

Из приведенного выше вывода очевидно, что токен на предъявителя и 'Content-Type': 'application/json' были переданы в качестве заголовка.

Тогда почему я получаю ошибкуво время запуска сценария?Может кто-нибудь помочь мне?

1 Ответ

2 голосов
/ 24 июня 2019

Скорее всего, тело вашего POST неправильно отформатировано. Когда я сделал это с библиотекой requests, я обнаружил, что мне нужно установить data примерно так:

data = json.dumps(email_msg)

По умолчанию для data используется кодирование формы, а не передача в формате JSON. Посмотрите на https://docs.microsoft.com/outlook/rest/python-tutorial#contents-of-tutorialoutlookservicepy

...