有没有一种方法可以检查特定请求ID的日志?

时间:2019-11-07 13:22:30

标签: ruby ruby-on-rails-5 microsoft-graph microsoft-graph-mail

我一直在订阅,以便在收到电子邮件时通知我的应用程序。为了激活订阅,我向https://graph.microsoft.com/v1.0/subscriptions发送了POST请求。每当我发送请求时,都会收到以下错误(请注意,从Graph Explorer尝试时,此方法可以正常工作):

response => {
    "error"=> {
        "code"=>"InvalidRequest", 
        "message"=>"Server could not process subscription creation payload.",
        "innerError"=> {
            "request-id"=>"e4a9aef5-89b0-4d9c-ac11-01e0c188ceee", 
            "date"=>"2019-11-07T13:15:50"
        }}}

代码示例:

GRAPH_HOST = 'https://graph.microsoft.com'.freeze

def mail_noti token

    query = { 
        "changeType": "created,updated",
        "notificationUrl": "https://0d9fdb76.ngrok.io/calendar/notif_sub",
        "resource": "me/mailFolders('Inbox')/messages",
        "clientState": "secretClientValue",
        "expirationDateTime": "2019-11-08T18:23:45.9356913Z",
        "Authorization": "Bearer #{token}",
        "Accept": 'application/json'
    }

    headers = {
        Authorization: "Bearer #{token}",
        Accept: 'application/json'
    }

    endpoint = '/v1.0/subscriptions'

    binding.pry

    a =  HTTParty.post("#{GRAPH_HOST}#{endpoint}", body: query, headers: headers)
end

2 个答案:

答案 0 :(得分:1)

body必须为application/json格式,但您是以application/x-www-form-urlencoded的形式发送的。您需要使用.to_json

HTTParty.post("#{GRAPH_HOST}#{endpoint}",
    :body => {
        "changeType": "created,updated",
        "notificationUrl": "https://0d9fdb76.ngrok.io/calendar/notif_sub",
        "resource": "me/mailFolders('Inbox')/messages",
        "clientState": "secretClientValue",
        "expirationDateTime": "2019-11-08T18:23:45.9356913Z",
        "Authorization": "Bearer #{token}",
        "Accept": 'application/json' 
    }.to_json,
    :headers => {
        Authorization: "Bearer #{token}",
        Accept: 'application/json',
        'Content-Type' => 'application/json'
    })

答案 1 :(得分:-1)

对我有用的最终有效载荷结构:

HTTParty.post("#{GRAPH_HOST}#{endpoint}", 
                    body:  {
                      expirationDateTime: "2019-11-09T18:23:45.9356913Z",
                      changeType: "created,updated",
                      resource: "me/mailFolders('Inbox')/messages",
                      notificationUrl: "https://e44e6608.ngrok.io/calendar/notif_sub",
                      clientState: "secretClientValue"
                    }.to_json, 
                    headers: {
                      Authorization: "Bearer #{token}",
                      Accept: 'application/json',
                      'Content-Type' => 'application/json'
                    }
                )

PS:每当发送有效载荷出现问题时,请确保您遵循以下步骤

  • 检查标题是否正确,尤其是“ Content-Type”和“ Authorization”(如果要直接发送http请求,则必须添加“ Key”:“ Authorization”和“ Value”:Bearer#{token} )
  • 检查您是否正在发送“身体”钥匙
  • 检查您是否在“ body”下发送了该API的所有必需键