Share

Authenticate and call various API endpoints (Helix)

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 need create an Authentication() instance that is used to self host and listen to events while waiting for a user to authenticate. 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 endpoints such as: