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