File: reuse-user-token.rst

package info (click to toggle)
python-twitchapi 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,144 kB
  • sloc: python: 6,877; javascript: 13; makefile: 11
file content (108 lines) | stat: -rw-r--r-- 3,343 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Reuse user tokens with UserAuthenticationStorageHelper
======================================================

In this tutorial, we will look at different ways to use :const:`~twitchAPI.oauth.UserAuthenticationStorageHelper`.

Basic Use Case
-------------- 

This is the most basic example on how to use this helper.
It will store any generated token in a file named `user_token.json` in your current folder and automatically update that file with refreshed tokens.
Should the file not exists, the auth scope not match the one of the stored auth token or the token + refresh token no longer be valid, it will use :const:`~twitchAPI.oauth.UserAuthenticator` to generate a new one.

.. code-block:: python
   :linenos:

    from twitchAPI import Twitch
    from twitchAPI.oauth import UserAuthenticationStorageHelper
    from twitchAPI.types import AuthScope

    APP_ID = 'my_app_id'
    APP_SECRET = 'my_app_secret'
    USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]


    async def run():
        twitch = await Twitch(APP_ID, APP_SECRET)
        helper = UserAuthenticationStorageHelper(twitch, USER_SCOPE)
        await helper.bind()
        # do things

        await twitch.close()


    # lets run our setup
    asyncio.run(run())


Use a different file to store your token
----------------------------------------

You can specify a different file in which the token should be stored in like this:


.. code-block:: python
   :linenos:
   :emphasize-lines: 4, 15

    from twitchAPI import Twitch
    from twitchAPI.oauth import UserAuthenticationStorageHelper
    from twitchAPI.types import AuthScope
    from pathlib import PurePath

    APP_ID = 'my_app_id'
    APP_SECRET = 'my_app_secret'
    USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]


    async def run():
        twitch = await Twitch(APP_ID, APP_SECRET)
        helper = UserAuthenticationStorageHelper(twitch,
                                                 USER_SCOPE, 
                                                 storage_path=PurePath('/my/new/path/file.json'))
        await helper.bind()
        # do things

        await twitch.close()


    # lets run our setup
    asyncio.run(run())


Use custom token generation code
--------------------------------

Sometimes (for example for headless setups), the default UserAuthenticator is not good enough.
For these cases, you can use your own function.

.. code-block:: python
   :linenos:
   :emphasize-lines: 10, 11, 12, 18

    from twitchAPI import Twitch
    from twitchAPI.oauth import UserAuthenticationStorageHelper
    from twitchAPI.types import AuthScope

    APP_ID = 'my_app_id'
    APP_SECRET = 'my_app_secret'
    USER_SCOPE = [AuthScope.CHAT_READ, AuthScope.CHAT_EDIT]


    async def my_token_generator(twitch: Twitch, target_scope: List[AuthScope]) -> (str, str):
        # generate new token + refresh token here and return it
        return 'token', 'refresh_token'

    async def run():
        twitch = await Twitch(APP_ID, APP_SECRET)
        helper = UserAuthenticationStorageHelper(twitch, 
                                                 USER_SCOPE, 
                                                 auth_generator_func=my_token_generator)
        await helper.bind()
        # do things

        await twitch.close()


    # lets run our setup
    asyncio.run(run())