Help Needed: (PHP) authenticated API requests with github example

Hello!
I am using the authentication example from github:
https://github.com/twitchdev/authentication-samples/tree/master/php

The example works, but when trying to create a custom authenticated API request…

        // You can now create authenticated API requests through the provider.
        $request = $provider->getAuthenticatedRequest(
           'GET',
           'https://api.twitch.tv/kraken/user',
           $accessToken
        );

… but var_export($request); returns something weird like this…

Output
GuzzleHttp\Psr7\Request::__set_state(array(
   'method' => 'GET',
   'requestTarget' => NULL,
   'uri' => 
  GuzzleHttp\Psr7\Uri::__set_state(array(
     'scheme' => 'https',
     'userInfo' => '',
     'host' => 'api.twitch.tv',
     'port' => NULL,
     'path' => '/kraken/user',
     'query' => '',
     'fragment' => '',
  )),
   'headers' => 
  array (
    'Host' => 
    array (
      0 => 'api.twitch.tv',
    ),
    'Client-ID' => 
    array (
      0 => '<Removed>',
    ),
    'Accept' => 
    array (
      0 => 'application/vnd.twitchtv.v5+json',
    ),
    'Authorization' => 
    array (
      0 => 'Bearer <Removed>',
    ),
  ),
   'headerNames' => 
  array (
    'client-id' => 'Client-ID',
    'accept' => 'Accept',
    'authorization' => 'Authorization',
    'host' => 'Host',
  ),
   'protocol' => '1.1',
   'stream' => NULL,
))

…and not something like this, which I was hoping to get:

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 130710009
                    [login] => cr4zyeasy
                    [display_name] => Cr4zyEasy
                    [type] => 
                    [broadcaster_type] => 
                    [description] => 
                    [profile_image_url] => https://static-cdn.jtvnw.net/jtv_user_pictures/cr4zyeasy-profile_image-a5d32b6de722b4d6-300x300.png
                    [offline_image_url] => 
                    [view_count] => 32
                )

        )

)

(I don’t want to do this request, it was just an example to simplify my problem)

What am I doing wrong?

Thanks in advance!

Been a while since I have used GuzzleHttp but I think you may want to do something like:

    $result = $response->getBody();
    $object = json_decode($result);

I seem to recall needing to get back the body object then decode that. Keep in mind, been a while, I could be very wrong!

EDIT

I think you are also just building the $request which is to be sent. What you are viewing is the request packet, (by the way - remove that Bearer token from the above screenshot). You need to send the request. Again, I haven’t touched GuzzleHttp in a while, so this could be wrong:

$client = new GuzzleHttp\Client(); 
$response = $client->send($request);  # Send your request that you built.
1 Like

Thanks for the answer.

I’m now using this code:

        $request = $provider->getAuthenticatedRequest(
           'GET',
           'https://api.twitch.tv/kraken/user',
           $accessToken
        );

        $client = new GuzzleHttp\Client();
        $response = $client->send($request);

        $result = $response->getBody();
        $object = json_decode($result);

        print $object;

Now I’m actually getting something - an error message:

Caught exception: Client error: `GET https://api.twitch.tv/kraken/user` resulted in a `401 Unauthorized` response: {"error":"Unauthorized","status":401,"message":"authentication failed"}

It usually should mean, that either the client id or secret is wrong, but it definetly isn’t, because the example script is working perfectly fine…

Example Script
        // Using the access token, get user profile
        $resourceOwner = $provider->getResourceOwner($accessToken);
        $user = $resourceOwner->toArray();

So, why do I get an error, even though my client id / secret is correct?

Now using cURL, which works perfectly fine.

#closed

1 Like

My only guess is that the header wasn’t being built right. Although, looks like you followed the directions. I wonder if you had tried without the Bearer token for laughs and double checked the headers being sent. Sorry things didn’t work out though! Glad you got cURL working.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.