Authentication Overview

To access your personal YouTube Music data (playlists, library, history), you need to authenticate your requests. YTMusicAPI PHP supports several authentication methods:

Security Note: Never commit authentication files to version control. Add them to your .gitignore file.

Browser Method (Recommended)

This is the most reliable method for getting authentication headers. You'll need Firefox for best results, though we have tried to make it work with Chrome as well.

Step 1: Extract Headers from Browser

  1. Open Firefox and go to https://music.youtube.com
  2. Make sure you're logged in to your Google account
  3. Open Developer Tools (F12 or Ctrl/Cmd + Shift + I)
  4. Go to the Network tab
  5. Click on "Library" in YouTube Music to trigger a request
  6. Filter by browse
  7. Right-click on a POST request under the name column
  8. Select Copy ValueCopy Request Headers

Step 2: Setup Authentication

Run the setup command:

php vendor/bin/setup-ytmusicapi browser

Paste the copied headers when prompted and press Enter twice. This will create a browser.json file.

Step 3: Use Authentication

<?php
require "vendor/autoload.php";

// Use the generated browser.json file
$yt = new Ytmusicapi\YTMusic("browser.json");

// Now you can access your personal data
$playlists = $yt->get_library_playlists();
$history = $yt->get_history();

For Chrome users or when the browser method doesn't work, you can use cookies directly.

Extract Cookies

  1. Go to https://music.youtube.com in your browser
  2. Open Developer Tools → Network tab
  3. Find a request to a browse or next endpoint
  4. Click on the request and go to the Headers tab
  5. Copy the Cookie value
  6. Copy the X-Goog-Authuser value (usually "0")

Use Cookie Authentication

<?php
require "vendor/autoload.php";

$cookie = "SAPISID=your_sapisid_value; __Secure-3PAPISID=your_secure_value; SID=your_sid_value; ...";
$user = "0"; // X-Goog-Authuser value

$yt = new Ytmusicapi\YTMusic($cookie, $user);

// Access authenticated endpoints
$playlists = $yt->get_library_playlists();
Required Cookies: Your cookie string must contain __Secure-3PAPISID, SAPISID, and SID values to work properly.

Manual Configuration File

Create a JSON file with the required authentication headers:

{
    "cookie": "SAPISID=your_value; __Secure-3PAPISID=your_value; SID=your_value; ...",
    "x-goog-authuser": "0",
    "x-goog-visitor-id": "your_value"
}

Save this as auth.json and use it:

$yt = new Ytmusicapi\YTMusic("auth.json");

OAuth Setup

We have recently found OAuth support to no longer be working in this library, both Python and PHP versions are failing.

Brand Accounts

Brand accounts are not yet supported.

Authentication Troubleshooting

Authentication Failed

Symptoms: "Please provide authentication before using this function"

Solution: Ensure your authentication file contains valid credentials and the required cookie values.

Missing SAPISID Cookie

Symptoms: "Your cookie is missing the required value __Secure-3PAPISID"

Solution: Re-extract cookies from a fresh browser session. Make sure you're logged in and have accessed YouTube Music recently.

HTTP 401 Unauthorized

Symptoms: Requests fail with 401 status

Solution: Your authentication has expired. Re-run the browser setup process to get fresh credentials.

HTTP 403 Forbidden

Symptoms: Requests fail with 403 status

Solution: Google may be blocking your IP or user agent. Try from a different network or with fresh authentication.

Testing Authentication

Test your authentication setup with this simple script:

<?php
require "vendor/autoload.php";

try {
    $yt = new Ytmusicapi\YTMusic("browser.json");

    // Test authenticated request
    $account = $yt->get_account();
    echo "Authentication successful!\n";
    echo "Account: " . $account->name . "\n";

} catch (Exception $e) {
    echo "Authentication failed: " . $e->getMessage() . "\n";
}
Tip: Keep your authentication files in a secure location and never share them. They provide access to your YouTube Music account.