Microsoft图无法读取JSON请求有效负载

时间:2018-08-07 08:50:32

标签: php laravel microsoft-graph guzzle azure-ad-graph-api

我正在尝试使用php中的Microsoft graph创建订阅,但是目前无法看到问题所在。

代码在以下位置中断:

protected $http_subscribe = "https://graph.microsoft.com/v1.0/subscriptions";

public function getSubscriptions()
{
    if(empty($this->token)) {
        dd('no token supplied'); //some debugging
    }

    $date = $this->endTimeDate->format('Y-m-d'); //This is Carbon date

    $time = $this->endTimeDate->format('H:i:s.u');

    $response = $this->client->request('POST', $this->http_subscribe, [
        'headers' => [
            'Authorization' => 'Bearer ' . $this->token,
            'Content-Type' => "application/json"
        ], 'form_params' => [
            "changeType" => "created,updated",
            "notificationUrl" => "https://website.test/notify",
            "resource" => "me/mailFolders('Inbox')/messages",
            "expirationDateTime" => $date.'T'.$time,
            "clientState" => "secretClientValue"
        ]
    ]);

    dd($response);
}

我得到的全部错误是:

"""
{\r\n
  "error": {\r\n
    "code": "BadRequest",\r\n
    "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",\r\n
    "innerError": {\r\n
      "request-id": "063d9947-80fd-461d-a70e-q0bd8eee9d56",\r\n
      "date": "2018-08-07T08:20:54"\r\n
   }\r\n
  }\r\n
}
"""

现在我明白了错误的意思,我的json无效,但是此数组采用正确的json格式,我将Guzzle用作客户端。

1 个答案:

答案 0 :(得分:3)

您应该使用json选项。试试这个:

$response = $this->client->request('POST', $this->http_subscribe, [
   'headers' => [
       'Authorization' => 'Bearer ' . $this->token,
   ],
   'json' => [
       "changeType" => "created,updated",
       "notificationUrl" => "https://website.test/notify",
       "resource" => "me/mailFolders('Inbox')/messages",
       "expirationDateTime" => $date.'T'.$time,
       "clientState" => "secretClientValue",
   ],
]);