Giter Site home page Giter Site logo

dekirai / twitch-auth-example Goto Github PK

View Code? Open in Web Editor NEW

This project forked from swiftyspiffy/twitch-auth-example

0.0 0.0 0.0 18 KB

This example shows how to create an oauth user access and refresh token using c# and TwitchLib

License: MIT License

C# 100.00%

twitch-auth-example's Introduction

Twitch Auth Example

A C# example of how to use Twitch's OAuth authorization code flow to generate a user access token (as well as refresh token). We'll be using TwitchLib.Api to make calls to Twitch.

Overview

Twitch's OAuth implementation is fairly standard is does not deviate from OAuth standards. At a high level, you'll create an application on Twitch's developer site. This will automatically create a client id, client secret, and you'll have the chance to setup the redirect uri (where the user goes after authenticating on Twitch's auth portal). For this example, you should use "http://localhost:8080/redirect/". When the example application is run, a local web server will be started, listening on port 8080, and will capture the user being bounced to http://localhost:8080/redirect/ post authorization. Twitch sends them to the redirect uri address, along with a few query string parameters, including code. We will extract the code value from the query string, and use it, in combination with the client secret, client id, and redirect uri to complete the authorization flow with Twitch. Twitch will return a payload include access_token, refresh_token, expires_in, scopes, and a few other fields. Most important to us are the tokens and the expires in value. The access token expires at the end of the number of seconds provided in expires_in. You should use the refresh token (which does not expire unless the user revokes access to the application) to request a new access token.

Developer Application

The first step is to create an application on Twitch's developer portal: https://dev.twitch.tv . Login with your Twitch account. After being logged in, on the right side click Register Your Application. Give it a name, and an oAuth redirect url of http://localhost:8080/redirect/. Select a category, and hit the Create button. Click into the application to view its details. You'll find the Client Id (note this), as well as the button called New Secret. Click the button to generate a new secret and note it. At this point you should have a client id and client secret.

Determine The Scope

OAuth uses granular scope values to determine what kind of priviledges and data an application has access to. For Twitch, you can find the list of available scopes at: https://dev.twitch.tv/docs/authentication#scopes . Note down the scopes you're interested in.

Setting Up The Example

In the Config.cs file, you'll find three fields: TwitchClientId, TwitchRedirectUri, and TwitchClientSecret. Fill in these fields (uri is http://localhost:8080/redirect/).

In Program.cs, around line 10, you'll find a private list variable named scopes containing a number of prepopulated scopes. Feel free to remove/update/change these to your liking.

Run The Example

Run the program. You'll see a couple things printed out, with the most recent being an authorization URL. Visit this URL. If you are not logged in, you'll be logged in. If you are logged in, you'll be asked to authorize your account against the application for the specified scopes. On authorization, you'll be bounced to http://localhost:8080/redirect/ where you should be met with Authorization started! Check your application!. On the application, you'll find something along the lines of:

Authorization success!

User: <username> (id: <userid>)
Access token: <access_token>
Refresh token: <refresh_token>
Expires in: <expires_in_seconds>
Scopes: <csv_of_scopes>

This example calls the Twitch api with the recently generated access token to discover the calling user (username, userid).

Packages Used

  • TwitchLib.Api: C# Twitch api library that wraps all Twitch helix, v5, auth and undocumented endpoints, as well as some helper third party stuff.

Contributors

Questions?

Ping me on Twitter: https://twitter.com/swiftyspiffy

twitch-auth-example's People

Contributors

swiftyspiffy avatar mahsaap avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.