| 1 |
Browser authentication |
| 2 |
====================== |
| 3 |
|
| 4 |
This method of authentication emulates your browser session by reusing its request headers. |
| 5 |
Follow the instructions to have your browser's YouTube Music session request headers parsed |
| 6 |
to a ``ytmusicapi`` configuration file. |
| 7 |
|
| 8 |
Copy authentication headers |
| 9 |
--------------------------- |
| 10 |
|
| 11 |
To run authenticated requests, set it up by first copying your request headers from an authenticated POST request in your browser. |
| 12 |
To do so, follow these steps: |
| 13 |
|
| 14 |
- Open a new tab |
| 15 |
- Open the developer tools (Ctrl-Shift-I) and select the "Network" tab (Command-Option-I on MacOS) |
| 16 |
- Go to https://music.youtube.com and ensure you are logged in |
| 17 |
- Find an authenticated POST request. The simplest way is to filter by ``/browse`` using the search bar of the developer tools. |
| 18 |
If you don't see the request, try scrolling down a bit or clicking on the **Library** button in the top bar. |
| 19 |
|
| 20 |
.. raw:: html |
| 21 |
|
| 22 |
<details open> |
| 23 |
<summary><a>Firefox (recommended)</a></summary> |
| 24 |
|
| 25 |
.. container:: |
| 26 |
|
| 27 |
- Verify that the request looks like this: **Status** 200, **Method** POST, **Domain** music.youtube.com, **File** ``browse?...`` |
| 28 |
- Copy the request headers (right click > copy > copy request headers) |
| 29 |
|
| 30 |
.. raw:: html |
| 31 |
|
| 32 |
</details> |
| 33 |
|
| 34 |
.. raw:: html |
| 35 |
|
| 36 |
<details> |
| 37 |
<summary><a>Chromium (Chrome/Edge)</a></summary> |
| 38 |
|
| 39 |
.. container:: |
| 40 |
|
| 41 |
- Verify that the request looks like this: **Status** 200, **Name** ``browse?...`` |
| 42 |
- Click on the Name of any matching request. In the "Headers" tab, scroll to the section "Request headers" and copy everything starting from "accept: \*/\*" to the end of the section |
| 43 |
- On new versions of Chrome, right click on the name and "Copy as fetch (Node.js)". Use a text editor to delete everything except the following: |
| 44 |
|
| 45 |
.. literalinclude:: headers_auth.json.example |
| 46 |
:language: JSON |
| 47 |
|
| 48 |
.. raw:: html |
| 49 |
|
| 50 |
</details><br> |
| 51 |
|
| 52 |
Using the headers in your project |
| 53 |
--------------------------------- |
| 54 |
|
| 55 |
To set up your project, open a console and call |
| 56 |
|
| 57 |
.. code-block:: bash |
| 58 |
|
| 59 |
ytmusicapi browser |
| 60 |
|
| 61 |
Follow the instructions and paste the request headers to the terminal input. |
| 62 |
|
| 63 |
If you don't want terminal interaction in your project, you can pass the request headers with the ``headers_raw`` parameter: |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
import ytmusicapi |
| 68 |
ytmusicapi.setup(filepath="browser.json", headers_raw="<headers copied above>") |
| 69 |
|
| 70 |
The function returns a JSON string with the credentials needed for :doc:`../usage`. Alternatively, if you passed the filepath parameter as described above, |
| 71 |
a file called ``browser.json`` will be created in the current directory, which you can pass to ``YTMusic()`` for authentication. |
| 72 |
|
| 73 |
These credentials remain valid as long as your YTMusic browser session is valid (about 2 years unless you log out). |
| 74 |
|
| 75 |
.. raw:: html |
| 76 |
|
| 77 |
<details> |
| 78 |
<summary><a>MacOS special pasting instructions</a></summary> |
| 79 |
|
| 80 |
.. container:: |
| 81 |
|
| 82 |
- MacOS terminal application can only accept 1024 characters pasted to std input. To paste in terminal, a small utility called pbpaste must be used. |
| 83 |
- In terminal just prefix the command used to run the script you created above with |
| 84 |
``pbpaste |`` |
| 85 |
- This will pipe the contents of the clipboard into the script just as if you had pasted it from the edit menu. |
| 86 |
|
| 87 |
.. raw:: html |
| 88 |
|
| 89 |
</details><br> |
| 90 |
|
| 91 |
Manual file creation |
| 92 |
-------------------- |
| 93 |
|
| 94 |
Alternatively, you can create your own file ``browser.json`` and paste the authentication and cookie: |
| 95 |
|
| 96 |
.. literalinclude:: headers_auth.json.example |
| 97 |
:language: JSON |