nicklaw5/twitch-api-php

Authorization problem when retrieving user data

Be-Mann opened this issue · 2 comments

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);
    }`

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.