AuthenticationΒΆ

For some methods of pytwitcherapi.TwitchSession, the user needs to grant pytwitcher authorization. Twitch Authentication is based on OAuth. We use the implicit grant workflow. In short, the user visits a website. Has to login, and allow pytwitcher. Twitch will redirect him to pytwitcherapi.constants.REDIRECT_URI. In the url fragment of that redirection, one can find the token we need. To make it simple for the user, here is what should be done for authentication:

  • Call pytwitcherapi.TwitchSession.start_login_server(). This will create a thread that serves a server on pytwitcherapi.constants.LOGIN_SERVER_ADRESS. Once the user gets redirected, this server will pick up the request and extract the token:

    1
    2
    3
    4
    import pytwitcherapi
    
    ts = pytwitcherapi.TwitchSession()
    ts.start_login_server()
    
  • Get the url pytwitcherapi.TwitchSession.get_auth_url() and send the user to visit that url in his favorite webbrowser. He might have to login, and allow pytwitcher, if he did not already:

    6
    7
    8
    import webbrowser
    url = ts.get_auth_url()
    webbrowser.open(url)
    
  • Wait until the user finished login in. Then call pytwitcherapi.TwitchSession.shutdown_login_server() to shutdown the server and join the thread:

    10
    11
    raw_input("Press ENTER when finished")
    ts.shutdown_login_server()
    
  • Check if the user authorized the session with pytwitcherapi.TwitchSession.authorized():

    13
    14
    assert ts.authorized, "Authorization failed! Did the user allow it?"
    print "Login User: %s" % ts.current_user
    
  • Now you can call methods that require authentication:

    16
    streams = ts.followed_streams()