Custom Follow Button

So I have been trying to make my own custom follower button for Twitch that would allow a user who comes to the site to be able to follow a specific channel that I want.

When I try this code out, I get a response in the console telling me:

[Twitch] API Error: Not Found; iammattbettiol is not following screwattack

Here is the code I’m currently using:

function follow(channel) {
    var success = function() {
        console.log("It worked");
    }
    var failure = function(step) {
        window.alert("Something went wrong at step: " + step);
    }

    Twitch.getStatus(function(error, status) {
        if (error) return failure("1");
        if (!status.authenticated) return failure("-1");

        Twitch.api({
            method: 'user'
        }, function(error, user) {
            if (error) return failure("2");

            Twitch.api({
                method: '/users/' + user.name + '/follows/channels/' + channel,
                verb: 'PUT'
            }, function(error, response) {
                if (error) return failure(user.name);
                success();
            });
        });
    });
}


$(function() {
    $('#follow-channel').click(function() {
        follow("screwattack");
    });

    window.CLIENT_ID = 'hgba5ruf675mvp2g6wiy1r0ynxwkpfd';

    Twitch.init({
        clientId: CLIENT_ID
    }, function(error, status) {
        if (status.authenticated) {
            $('.status input').val('Logged in! Allowed scope: ' + status.scope);
            $('.authenticated').removeClass('hidden');
        } else {
            $('.status input').val('Not Logged in! Better connect with Twitch!');
            $('.authenticate').removeClass('hidden');
        }
    });

    $('.twitch-connect').click(function() {
        Twitch.login({
            scope: ['user_follows_edit', 'user_read']
        }, function(error, status) {
            
        });
    });

    $('#logout button').click(function() {
        Twitch.logout();
        window.location = window.location.pathname;
    })
});

I was able to get in touch with my buddy who is very well educated with stuff like this.

He ended downloading the twitch-js-sdk file and had to make some changes himself.

He noted that the file (twitch.js) had a bug in it where they weren’t even applying the PUT method to the ajax request, so it was sending a GET request instead.

With his change applied, everything works like I want it to using the originally posted code.

The code on the CDN is outdated as far as I know, and you should download and use the version on github instead.

We were using the GitHub version:
https://www.diffnow.com/?report=zjd8a

JSONP can only use GET since it uses the script tags instead of XHR. The JS SDK sets the method in the _method query param. If it’s not working, it’s a problem with the API not accepting it.

i.e. the actual request is something along the lines of what’s below, which works for me.

GET /kraken/users/:user/follows/channels/:channel?callback=something&_method=PUT&oauth_token=some_valid_token

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