Getting "follows" after Login

Hello everyone sorry for my bad english,

I hope you’re all doing well. I have a coding issue that I’d like to discuss and seek some guidance on. I’m currently working on a project where I want to implement a feature similar to Twitch’s follow list. Specifically, I’m aiming to create a sidebar that displays a list of users whom the visitor is following.

The main functionality I’m trying to achieve is as follows: when a visitor clicks on a designated button, I want them to grant permission to my website. Once this permission is granted, I intend to display a list of their follows on the sidebar, mimicking the layout that Twitch uses.

I’ve been going through the documentation and trying various approaches, but I’ve hit a bit of a roadblock. If any of you have experience with implementing this type of functionality or if you have suggestions on how I can approach this problem, I would greatly appreciate your input.

Thank you in advance for your assistance and insights. Looking forward to hearing your thoughts on this!

This is my code:

Index.html:

<?php
// callback.php
if (isset($_GET['code'])) {
    $client_id = "XXXX";
    $client_secret = "XXXX";
    $redirect_uri = "XXXX..../callback.php";
    $code = $_GET['code'];

    $token_url = "https://id.twitch.tv/oauth2/token";
    $params = array(
        "client_id" => $client_id,
        "client_secret" => $client_secret,
        "code" => $code,
        "grant_type" => "authorization_code",
        "redirect_uri" => $redirect_uri
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $token_url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    curl_close($ch);

    $token_data = json_decode($response, true);

    if (isset($token_data['access_token'])) {
        $access_token = $token_data['access_token'];

        $api_url = "https://api.twitch.tv/helix/users/follows";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api_url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Client-ID: " . $client_id,
            "Authorization: Bearer " . $access_token
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);

        $data = json_decode($response, true);

        if (isset($data["data"]) && !empty($data["data"])) {
            echo "<h2>Streamern, denen du folgst:</h2>";
            echo "<ul>";
            foreach ($data["data"] as $follow) {
                echo "<li>" . $follow["to_name"] . "</li>";
            }
            echo "</ul>";
        } else {
            echo "NO FOLLOWS";
        }
    } else {
        echo "GET TOKEN ERROR";
    }
} else {
    echo "AUTH ERROR";
}
?>

auth.php

<?php

$client_id = "XXXX";
$redirect_uri = "XXXX";
$scope = "user:read:follows";

$auth_url = "https://id.twitch.tv/oauth2/authorize" .
            "?client_id={$client_id}" .
            "&redirect_uri={$redirect_uri}" .
            "&response_type=code" .
            "&scope={$scope}";

header("Location: $auth_url");
exit();
?>

When I click on the login button, I am successfully redirected. I can then grant permission to the follows. However, after the successful redirect, no list is displayed. I always see the message “NO FOLLOWS”.

If you can help me I would be grateful.

This call results in a HTTP 4xx, but you don’t test the resulting HTTP Code

This URL is incomplete you didn’t specify a user_id in your query string parameters, so the API didn’t know whose follows to return

Only the Get Users API support “look up via token”

So you’d want to

You should also update your PHP code to be more error tolerant.
You don’t test if your get HTTP 200 response
You don’t test if your JSON Decode succeeded

A code example that might be of use https://github.com/BarryCarlyon/twitch_misc/blob/main/authentication/user_access_generator/php/index.php#L38

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