使用GuzzleHttp访问令牌为空

问题描述 投票:1回答:1

当我尝试使用以下代码建立与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

调试给了我这个:

  • 即将connect()连接到graph.microsoft.com端口443(#0)
  • 尝试40.126.9.112 ......
  • 连接到graph.microsoft.com(40.126.9.112)端口443(#0)
  • 使用certpath初始化NSS:sql:/ etc / pki / nssdb
  • CAfile:/etc/pki/tls/certs/ca-bundle.crt CApath:none
  • 使用TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384进行SSL连接
  • 服务器证书:
  • 主题:CN = graph.microsoft.com
  • 开课日期:1月27日19:09:45 GM9 GMT
  • 到期日:1月27日19:09:45 2021 GMT
  • 通用名称:graph.microsoft.com
  • 发行人: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主机:graph.microsoft.com

<HTTP / 1.1 401 Unauthorized <Content-Type:application / json; charset = utf-8 <request-id:c82bfd7f-921f-40b7-a973-38b6630cb2c2 <client-request-id:c82bfd7f-921f-40b7-a973-38b6630cb2c2 <x-ms-ags-diagnostic:{“ServerInfo”:{ “DataCenter”:“West Europe”,“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”<Strict-Transport-Security :max-age = 31536000 <Date:Tue,09 Apr 201 08:08:39 GMT <Content-Length:234

不知何故,当我给出相同的细节时,它确实可以在Postman中工作,但是你可以看到,Bearer之后的代码是空的。我确实检查了$this->getAccessToken(),它肯定包含令牌。

我在这做错了什么?

php microsoft-graph guzzle guzzlehttp
1个回答
3
投票
        $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]);

你的网址后面有一个点而不是你需要一个逗号。另外你的$ headers数组应该是格式化的“$key => $values”关系,可以在guzzle请求中传递。

© www.soinside.com 2019 - 2024. All rights reserved.