2016-11-30 66 views
0

我正在使用下面的代碼在python中使用outlook rest api發送郵件。在python中使用outlook rest api發送郵件時出錯

outlookservice.py

import requests 
import uuid 
import json 

outlook_api_endpoint = 'https://outlook.office.com/api/v2.0{0}' 

# Generic API Sending 
def make_api_call(method, url, token, user_email, payload = None, parameters = None): 
    # Send these headers with all API calls 
    headers = { 'User-Agent' : 'python_tutorial/1.0', 
       'Authorization' : 'Bearer {0}'.format(token), 
       'Accept' : 'application/json', 
       'X-AnchorMailbox' : user_email } 

    # Use these headers to instrument calls. Makes it easier 
    # to correlate requests and responses in case of problems 
    # and is a recommended best practice. 
    request_id = str(uuid.uuid4()) 
    instrumentation = { 'client-request-id' : request_id, 
         'return-client-request-id' : 'true' } 

    headers.update(instrumentation) 

    response = None 

    if (method.upper() == 'GET'): 
     response = requests.get(url, headers = headers, params = parameters) 
    elif (method.upper() == 'DELETE'): 
     response = requests.delete(url, headers = headers, params = parameters) 
    elif (method.upper() == 'PATCH'): 
     headers.update({ 'Content-Type' : 'application/json' }) 
     response = requests.patch(url, headers = headers, data = json.dumps(payload), params = parameters) 
    elif (method.upper() == 'POST'): 
     headers.update({ 'Content-Type' : 'application/json' }) 
     response = requests.post(url, headers = headers, data = json.dumps(payload), params = parameters) 

    return response 


def get_me(access_token): 
    get_me_url = outlook_api_endpoint.format('/Me') 

    # Use OData query parameters to control the results 
    # - Only return the DisplayName and EmailAddress fields 
    query_parameters = {'$select': 'DisplayName,EmailAddress'} 

    r = make_api_call('GET', get_me_url, access_token, "", parameters = query_parameters) 

    if (r.status_code == requests.codes.ok): 
    return r.json() 
    else: 
    return "{0}: {1}".format(r.status_code, r.text) 

def get_my_messages(access_token, user_email): 
    get_messages_url = outlook_api_endpoint.format('/Me/MailFolders/Inbox/Messages') 

    query_parameters = {'$top': '10', 
         '$select': 'ReceivedDateTime,Subject,From', 
         '$orderby': 'ReceivedDateTime DESC'} 

    r = make_api_call('GET', get_messages_url, access_token, user_email, parameters = query_parameters) 

    if (r.status_code == requests.codes.ok): 
    return r.json() 
    else: 
    return "{0}: {1}".format(r.status_code, r.text) 


def send_message(access_token,user_email): 
    get_messages_url = outlook_api_endpoint.format('/Me/sendmail') 
    query_parameters = { 
    "Message": { 
    "Subject": "Meet for lunch?", 
    "Body": { 
     "ContentType": "Text", 
     "Content": "The new cafeteria is open." 
    }, 
    "ToRecipients": [ 
     { 
     "EmailAddress": { 
      "Address": "[email protected]" 
     } 
     } 
    ], 
    "Attachments": [ 
     { 
     "@odata.type": "#Microsoft.OutlookServices.FileAttachment", 
     "Name": "menu.txt", 
     "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk=" 
     } 
    ] 
    }, 
    "SaveToSentItems": "false" 
    } 

    r = make_api_call('POST', get_messages_url, access_token, user_email, parameters = query_parameters) 
    if (r.status_code == requests.codes.ok): 
     print r.json() 
     return r.json() 
    else: 
     return "{0}: {1}".format(r.status_code, r.text) 

views.py

from django.shortcuts import render 
from django.http import HttpResponse, HttpResponseRedirect 
from django.core.urlresolvers import reverse 
from tutorial.authhelper import get_signin_url,get_token_from_code, get_token_from_refresh_token, get_access_token 
import requests 
import time 
from tutorial.outlookservice import get_me,get_my_messages,send_message 

# Create your views here. 

def home(request): 
    redirect_uri = "http://localhost:8000/tutorial/gettoken/" 
    sign_in_url = get_signin_url(redirect_uri) 
    return HttpResponse('<a href="' + sign_in_url +'">Click here to sign in and view your mail</a>') 

def gettoken(request): 
    auth_code = request.GET['code'] 
    redirect_uri = "http://localhost:8000/tutorial/gettoken/" 
    token = get_token_from_code(auth_code, redirect_uri) 
    access_token = token['access_token'] 
    user = get_me(access_token) 
    refresh_token = token['refresh_token'] 
    expires_in = token['expires_in'] 

    expiration = int(time.time()) + expires_in - 300 

    # Save the token in the session 
    request.session['access_token'] = access_token 
    request.session['refresh_token'] = refresh_token 
    request.session['token_expires'] = expiration 
    request.session['user_email'] = user['EmailAddress'] 
    return HttpResponse('User Email: {0}, Access token: {1}'.format(user['EmailAddress'], access_token)) 

這是send_message被稱爲

def mail(request): 

    access_token = get_access_token(request,request.build_absolute_uri(reverse('tutorial:gettoken'))) 
    user_email = request.session['user_email'] 

    if not access_token: 
    return HttpResponseRedirect(reverse('tutorial:home')) 
    else: 
    abc = send_message(access_token,user_email) 
    return HttpResponse(abc) 

我收到以下錯誤,而在Django編譯它的功能

400:{「error」:{「code」:「RequestBroker-ParseUri」,「message」:「查詢選項'Message'被指定了多次,但最多隻能指定一次。」}}

+0

請您分享請求正文並請求uri?正如在電線上看到的那樣 – Yogesh

回答

0

我知道這有點舊,但是您是否嘗試從send_message方法中的query_parameters JSON中刪除「消息」字段? 嘗試創建您的JSON,就好像Message字段已經存在一樣,您只需要提供/填充內部字段。就像這樣:

query_parameters = { "Subject": "Meet for lunch?", "Body": { "ContentType": "Text", "Content": "The new cafeteria is open." }, "ToRecipients": [ { "EmailAddress": { "Address": "[email protected]" } } ], "Attachments": [ { "@odata.type": "#Microsoft.OutlookServices.FileAttachment", "Name": "menu.txt", "ContentBytes": "bWFjIGFuZCBjaGVlc2UgdG9kYXk=" } ] }

0

在outlookservice.py你SEND_EMAIL功能,你發出的信息內容make_api_call作爲參數,而不是作爲有效載荷。以下內容可以解決您的問題:

r = make_api_call('POST', get_messages_url, access_token, user_email, payload = query_parameters) 
相關問題