GuzzleHttp 的访问令牌为空

Access token is empty with GuzzleHttp

当我尝试使用以下代码连接到 Microsoft Graph 时:

$headers = [
    'Authorization: Bearer ' . $this->getAccessToken(),
    'Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8',
    'Preference-Applied: odata.track-changes'
];
$response = $this->guzzle->request('GET', 'https://graph.microsoft.com/v1.0/me/calendarview/delta?startdatetime=2017-12-12T00:00:00Z&enddatetime=2020-12-13T00:00:00Z'. ['headers' => $headers], ['debug' => true]);

这给了我错误: Access token is empty

调试给了我这个:

  • About to connect() to graph.microsoft.com port 443 (#0)
  • Trying 40.126.9.112...
  • Connected to graph.microsoft.com (40.126.9.112) port 443 (#0)
  • Initializing NSS with certpath: sql:/etc/pki/nssdb
  • CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none
  • SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
  • Server certificate:
  • subject: CN=graph.microsoft.com
  • start date: Jan 27 19:09:45 2019 GMT
  • expire date: Jan 27 19:09:45 2021 GMT
  • common name: graph.microsoft.com
  • issuer: CN=Microsoft IT TLS CA 2,OU=Microsoft IT,O=Microsoft Corporation,L=Redmond,ST=Washington,C=US

    GET /v1.0/me/calendarview/delta?startdatetime=2017-12-12T00:00:00Z&enddatetime=2020-12-13T00:00:00ZArray HTTP/1.1 User-Agent: GuzzleHttp/6.3.3 curl/7.29.0 PHP/5.6.40 Host: graph.microsoft.com

< HTTP/1.1 401 未授权 < 内容类型:application/json; charset=utf-8 < 请求 ID: c82bfd7f-921f-40b7-a973-38b6630cb2c2 < 客户端请求 ID:c82bfd7f-921f-40b7-a973-38b6630cb2c2 < x-ms-ags-诊断:{"ServerInfo":{"DataCenter":"西 欧洲","Slice":"SliceC","Ring":"5","ScaleUnit":"002","RoleInstance":"AGSFE_IN_45","ADSiteName":"WEU"}} < WWW-Authenticate: Bearer realm="", authorization_uri="https://login.microsoftonline.com/common/oauth2/authorize", client_id="00000003-0000-0000-c000-000000000000" < 严格运输安全:max-age=31536000 < 日期:2019 年 4 月 9 日,星期二 08:08:39 GMT < 内容长度:234

当我提供相同的详细信息时,它在 Postman 中确实有效,但是如您所见,Bearer 之后的代码是空的。我确实检查了 $this->getAccessToken(),它肯定包含令牌。

我做错了什么?

        $headers = [
        "Authorization" => "Bearer". $token,
        'Content-Type' => 'application/json'
    ];

$response = $this->guzzle->request('GET', 'https://graph.microsoft.com/v1.0/me/calendarview/delta?startdatetime=2017-12-12T00:00:00Z&enddatetime=2020-12-13T00:00:00Z', ['headers' => $headers], ['debug' => true]);

你的 URL 后面有一个点,而不是你需要一个逗号。此外,您的 $headers 数组的格式应类似于“$key => $values”关系,以便在 guzzle 请求中传递。