Sinan Samet
Sinan Samet

Reputation: 6752

Access token is empty with GuzzleHttp

When I try to make a connection to Microsoft Graph with the following code:

$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]);

Which gives me the error: Access token is empty

And the debug gives me this:

  • 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 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 2019 08:08:39 GMT < Content-Length: 234

Somehow it does work in Postman when I give the same details however like you can see, the code after Bearer is empty. I did check $this->getAccessToken() and it definitely contains the token.

What am I doing wrong here?

Upvotes: 1

Views: 1880

Answers (1)

pr1nc3
pr1nc3

Reputation: 8348

        $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]);

You had a dot after your URL instead you needed a comma. Also your $headers array should be format like "$key => $values" relation to be passed in the guzzle request.

Upvotes: 4

Related Questions