【问题标题】:Is there a way to check logs for a particular request-id?有没有办法检查特定请求 ID 的日志?
【发布时间】:2019-11-07 13:22:30
【问题描述】:

我一直致力于订阅,以便在收到电子邮件时通知我的应用程序。为了激活订阅,我向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

【问题讨论】:

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


    【解决方案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'
        })
    

    【讨论】:

    • 我已经这样做了。如果我这样做,则无法识别密钥。 {"error"=> { "code"=>"InvalidRequest", "message"=>"expirationDateTime 是创建订阅的必需属性。", "innerError"=> { "request-id"=>"4c7557cb-82f4 -46b4-9d2a-449a62422165", "日期"=>"2019-11-08T06:09:18" } } }
    • 这对我有用。将 Content-Type 添加到标题。标头:{ 授权:“Bearer #{token}”,接受:'application/json','Content-Type' => 'application/json' }
    • 参考:github.com/microsoftgraph/microsoft-graph-docs/issues/… 我不知道为什么图表一开始没有向我发送“内容类型”标题丢失错误。它本可以节省很多时间。
    • 我已更新我的答案以反映 Content-Type。我希望 HTTParty 会根据内容自动设置 Content-Type,但看起来情况并非如此。
    【解决方案2】:

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

    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'键
    • 检查您是否在“body”下为该 API 发送所有必要的密钥

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多