nicklaw5 / twitch-api-php

A Twitch API client for PHP.
https://packagist.org/packages/nicklaw5/twitch-api-php
MIT License
116 stars 48 forks source link

Authorization problem when retrieving user data #155

Open Be-Mann opened 1 year ago

Be-Mann commented 1 year ago

Hi, I tried to retrieve the user data, but unfortunately I always get a fatal error

Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: GET https://api.twitch.tv/helix/users resulted in a 401 Unauthorized response: {"error": "Unauthorized", "status":401, "message": "Client ID and OAuth token do not match"}

Here is the code snippet I use:

`

    $code = (isset($_GET['code']) and $_GET['code']) ? $_GET['code'] : null;
    try {
        $token = $oauth->getUserAccessToken($code, $login_query);
        // It is a good practice to check the status code when they've responded, this really is optional though
        if ($token->getStatusCode() == 200) {
            // Below is the returned token data
            $data = json_decode($token->getBody()->getContents());

            // Your bearer token
            $twitch_access_token = $data->access_token ?? null;

        } else {
        //TODO: Handle Error
        }
    } catch (Exception $e) {
        //TODO: Handle Error
        var_dump($e);
    }

    try {
        // Make the API call. A ResponseInterface object is returned.
        $response = $twitchlogin->getUsersApi()->getUserByAccessToken($twitch_access_token);

        // Get and decode the actual content sent by Twitch.
        $responseContent = json_decode($response->getBody()->getContents());

        // Return the first (or only) user.
        return $responseContent->data[0];
    } catch (GuzzleException $e) {
        //TODO: Handle Error
        var_dump($e);
    }`
Be-Mann commented 1 year ago

In "RequestGenerator" $headers['client-id'] must be added to the header, then it works, but I didn't manage to retrieve the $clientId from OauthApi, I'm not so into object oriented programming.

Be-Mann commented 6 months ago

Wanted to ask if there will be a fix? @nicklaw5