Home DevelopersExamples & updates Authenticate as a user to use the API

Authenticate as a user to use the API

by BlushyFace
36 views

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).

Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments

Related Articles

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

LIVE NOW! CLICK TO VIEW.
CURRENTLY OFFLINE