Home » BlushyFace.Twitch.* » BlushyFace.Twitch.* examples » Authenticate as a user to use the API
BlushyFace.Twitch.* examples

Authenticate as a user to use the API

In this example we authenticate as a user and call a couple API (Helix) endpoints to get the user follower list and stream key.

First we wait for a user to authenticate and when successful we call the GetUserFollowsAsync() and GetStreamKeyAsync() methods to get the data from the Twitch API.

using System;
using System.Collections.Generic;
using BlushyFace.Twitch.API.Helix;
using BlushyFace.Twitch.Authentication.Enums;

namespace BlushyFace.Twitch.Tester.Tests
{
    public class Api
    {
        private Authentication _auth;

        public Api()
        {
            Run();
        }

        public void Run()
        {
            _auth = new Authentication(new[] { "http://+:8081/" });
            var scopes = new List<Enum>()
                         { 
                             Scope.Helix.UserReadEmail,
                            // Scope.Helix.ChannelReadStreamKey,
                         };

            _auth.OAuth.OpenBrowserAuthorizationFlow(scopes: scopes, addState: false, forceVerify: false);
            _auth.OnTokenResponse += Auth_OnTokenResponse;
        }

        private async void Auth_OnTokenResponse(object sender, Dictionary<string, string> tokenData)
        {
            var helixRequest = new HelixRequests(_auth.Settings.ClientId, tokenData["accessToken"]);

            // get authenticated user follower list
            var userFollowers = await helixRequest.Users.GetUserFollowsAsync(toId: tokenData["userId"]);

            // get authenticated user stream key, this will fail because of missing scope
            var streamKey = await helixRequest.Streams.GetStreamKeyAsync(tokenData["userId"]);
        }
    }
}

Here we see the returned data from the endpoint.

When a scope that is required to call an API endpoint is not provided you can see additional info in UnknownData why it failed.

 

BlushyFace.Twitch.API lib provides support for various Helix endpoints but note that it lacks most of the Kraken API endpoints because there is an alternative available in Helix or simply no demand to add them in (if you want to use specific Kraken v5 endpoints that are not available just let me know so I can add them in).

About the author

BlushyFace

Herrow, I'm BlushyFace and you are you. I also stream @ Twitch channel community.

Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
oldest
newest most voted
Inline Feedbacks
View all comments
dazaijunbot
dazaijunbot
1 month ago

Hello Sir Blushyface,

Is it okay to ask for an approach on how are you going to implement a cluster mode with tmi.js?