Twitch API with Laravel

hello i recently moved over to laravel and i had this working then not working and its really stressing me out where am i going wrong?

this is to get the access token

private function getTwitchAccessToken() {
    try {
        $response = Http::asForm()->post('https://id.twitch.tv/oauth2/token', [
            'client_id' => config('twitch.client_id'),
            'client_secret' => config('twitch.client_secret'),
            'grant_type' => 'client_credentials',
        ]);

        if ($response->successful()) {
            $accessToken = $response->json('access_token');
            return $accessToken;
        } else {
            $error = 'An error occurred while obtaining the access token: ' . $response->body();
            return $error;
        }
    } catch (\Exception $e) {
        // Log or print the error details
        \Illuminate\Support\Facades\Log::error('Twitch API Token Request Error:', [
            'URL' => 'https://id.twitch.tv/oauth2/token',
            'Error' => $e->getMessage(),
        ]);

        $error = 'An error occurred while making the Twitch API token request.';
        return $error;
    }
}

then i have get user id

function getUserID($twitchUsername) {
    try {
       $twitchAccessToken = $this->getTwitchAccessToken();
        $response = Http::withHeaders([
            'client_id' => config('twitch.client_id'),
            'Authorization' => 'Bearer '. $twitchAccessToken,
        ])->get('https://api.twitch.tv/helix/users?login=' . $twitchUsername);

        // Dump and die the response body for debugging
        return $response->body();

        if ($response->successful()) {
            $userData = $response->json('data.0');
            $userId = $userData['id'] ?? null;
            return $userId;
        } else {
            $error = 'An error occurred fetching user data.';
            print_r($error);
        }
    } catch (\Exception $e) {
        // Log or print the error details
        dd($e->getMessage());

        // You might want to redirect or display an error message to the user.
    }
}

and to get the videos i use

$twitchAccessToken = $this->getTwitchAccessToken();
$twitchid = $this->getUserID($channelID);

if ($twitchAccessToken && $twitchid) {
    $response = Http::withHeaders([
       'client_id' => config('twitch.client_id'),
        'Authorization' => 'Bearer ' . $twitchAccessToken,
    ])->get('https://api.twitch.tv/helix/videos', [
        'user_id' => $twitchid,
        'first' => 50,
        'sort' => 'time',
        'type' => 'archive',
    ]);

    if ($response->successful()) {
        $twitchvideos = $response->json('data');
        $results = $twitchvideos;
        $viewName = 'twitch-videos';
    } else {
        $error = 'An error occurred while fetching videos: ' . $response->body();
        return $error;
    }
} else {
    $error = 'Failed to retrieve Twitch user ID or access token.';
    return $error;
}

and this is what i get

An error occurred while fetching videos: {"error":"Service Unavailable","status":503,"message":""}

its probably something small like is this a twitch error or my code wrong?

never mind

the problem was the accesskey missmatch. Problem solved

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