Twitch IRC OAUTH

By SReject on Apr 30, 2015

So I got bored and figured out how to use OAUTH and twitch.tv's api to get the required password to connect your irc client to twitch's irc. This could easily be extended to allow for other authorization scopes

Setup:

  1. Login to twitch.tv for which you wish to create an irc oauth token for
  2. Go to http://www.twitch.tv/settings/connections
  3. Scroll down to "Register your application"
  4. Enter a unique name for your app under "Name"
  5. Enter the following url under "Redirect URI"*: http://localhost:7072
  6. Click the "Register" button
  7. Look under "USER EDITS" in this script and replace REDIRECT_URI with the URI you entered at step #5
  8. Copy the text within the box under "Client ID"
  9. Look under "USER EDITS" in this script and replace CLIENT_ID with the copied text
  10. Click "Secret"
  11. Copy the text within the box under "Client Secret"
  12. Look under "USER EDITS" in this script and replace CLIENT_SECRET with the text copied

*: The port does not have to be 7072, if you wish to use another you may, so long as the port isn't being used by another program on your system

Usage:

Once logged in, in mIRC type: /GetChatAccess
Follow the steps that appear, and if no errors occur, you'll get your access token

Code:

;--------------------;
;---- USER EDITS ----;
;--------------------;

; Edit this line with your app's "Redirect URI"
alias -l app_uri return REDIRECT_URI

; Edit this line with your app's "Client ID"
alias -l app_id return CLIENT_ID

; Edit this line with your app's "Clinet Secret"
alias -l app_secret return CLIENT_SECRET

;------------------------;
;---- END USER EDITS ----;
;------------------------;

alias GetChatAccess {
  if (!$portfree($app_port)) {
    echo -s 04[GetChatOAuth]: Unable to start temporary web server due to the port being in use and thus can't continue.
  }
  else {
    socklisten Twitch_GetChatAccess $app_port
    var %url = https://api.twitch.tv/kraken/oauth2/authorize?response_type=code&client_id= $+ $app_id $+ &redirect_uri= $+ $app_uri $+ &scope=chat_login
    url -a %url
  }
}

alias GetChatAccessStop {
  sockclose Twitch_GetChatAccess*
}
alias -l app_port return $gettok($app_uri, -1, 58)
alias -l urlencode return $regsubex($1-, /([^a-z\d_.-])/g, % $+ $base($asc(\t), 10, 16, 2))
alias -l cleanup { if ($sock($1)) { sockclose $v1 } | if ($hget($1)) { hfree $v1 } }
alias swcheck {
  if ($sock($1)) {
    if (!$sock($1).sq) {
      sockclose $sockname
    }
    else {
      .timer -io 1 1 sockwritecheck $1
    }
  }
}
on *:SOCKLISTEN:Twitch_GetChatAccess:{
  if (!$sockerr) {
    var %s = 1
    while ($sock($sockname $+ _ $+ %s)) { inc %s }
    %s = $sockname $+ _ $+ %s
    sockaccept %s
  }
}
on *:SOCKWRTIE:Twitch_GetChatAccess_*:{
  if (!$sock($sockname).sq && $sock($sockname).mark) {
    sockclose $sockname
  }
}
on *:SOCKREAD:Twitch_GetChatAccess_*:{
  if (!$sockerr) {
    var %t, %s = 1, %o, %d = Good - Now Check mIRC
    sockread %t
    %t = $regsubex(%t, /(?:^[\r\n]+)|(?:[\r\n]+$)/g, )
    if (!$sock($sockname).mark) {
      if (GET ?* HTTP/?* iswm %t && $regex(%t, /[?&]code=([^ &?]+)/i)) {
        if ($sock(Twitch_GetChatAccess)) { sockclose Twitch_GetChatAccess }
        var %s = sockwrite -n $sockname
        %s HTTP/1.1 200 OK
        %s Content-Type: text/plain
        %s Connection: close
        %s Content-Length: $len(%d)
        %s
        sockwrite $sockname %d
        %s = 1
        while ($sock(Twitch_GetChatAccessToken_ $+ %s)) { inc %s }
        %s = Twitch_GetChatAccessToken_ $+ %s
        sockopen -e %s api.twitch.tv 443
        sockmark %s $regml(1)
      }
      else {
        var %s = sockwrite -n $sockname
        %s HTTP/1.1 404 NOT_FOUND
        %s Content-type: text/plain
        %s Connection: close
        %s Content-Length: 0
        %s
      }
      sockmark $sockname close
      .timer -io 1 1 swCheck $sockname
    }
  }
}
on *:SOCKOPEN:Twitch_GetChatAccessToken_*:{
  if ($sockerr) {
    echo -s 04[ $+ $sockname $+ ]: Unable to connect to api.twitch.tv
    cleanup $sockname
  }
  else {
    var %p = $sock($sockname).mark, %s = sockwrite -n $sockname
    %p = client_id= $+ $app_id $+ &client_secret= $+ $app_secret $+ &grant_type=authorization_code&redirect_uri=  $+ $app_uri $+ &code= $+ $sock($sockname).mark
    %s POST /kraken/oauth2/token HTTP/1.1
    %s Host: api.twitch.tv
    %s Referer: http://api.twitch.tv
    %s Connection: close
    %s Content-Length: $len(%p)
    %s Content-Type: application/x-www-form-urlencoded
    %s
    sockwrite $sockname %p
  }
}
on *:SOCKWRITE:Twitch_GetChatAccessToken_*:{
  if ($sockerr) {
    echo -s 04[ $+ $sockname $+ ]: Unable to send data to api.twitch.tv
    cleanup $sockname
  }
}
on *:SOCKREAD:Twitch_GetChatAccessToken_*:{
  if ($sockerr) {
    echo -s 04[ $+ $sockname $+ ]: Unable to read data from api.twitch.tv
    cleanup $sockname
  }
  else {
    noop $hget($sockname, read, &read)
    sockread &tmp
    bcopy -c &read $calc($bvar(&read, 0) + 1) &tmp 1 -1
    hadd -mb $sockname read &read
  }
}
on *:SOCKCLOSE:Twitch_GetChatAccessToken_*:{
  if ($sockerr) {
    echo -s 04[ $+ $sockname $+ ]: Connection to api.twitch.tv closed unexpectedly
  }
  elseif (!$hget($sockname, read, &read)) {
    echo -s 04[ $+ $sockname $+ ]: No data read from api.twitch.tv
  }
  elseif (HTTP/1.1 200 OK $+ $crlf != $bvar(&read, 1, 17).text) {
    echo -s 04[ $+ $sockname $+ ]: Server Reported Error
  }
  elseif ($bfind(&read, 1, $crlf $+ $crlf)) {
    bcopy -c &read 1 &read $calc($v1 + 4) -1
    noop $regex($bvar(&read, 1-).text, /"access_token":\s*"([^"]+)"/)
    if ($regml(1)) {
      echo -s 12[ $+ Chat OAuth Token $+ ]: oauth: $+ $v1
    }
    else {
      echo -s 04[ $+ $sockname $+ ]: No access token in response
    }
  }
  else {
    echo -s 04[ $+ $sockname $+ ]: Server returned invalid response
  }
  cleanup $sockname
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.