Authentication Overview
To access your personal YouTube Music data (playlists, library, history), you need to authenticate your requests. YTMusicAPI PHP supports several authentication methods:
🌐 Browser Headers
Extract authentication from browser developer tools (recommended)
🍪 Cookie String
Use cookies directly from your browser session
📄 Manual Config
Create JSON configuration with required headers
🔐 OAuth
Full OAuth flow support
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
- Open Firefox and go to https://music.youtube.com
- Make sure you're logged in to your Google account
- Open Developer Tools (F12 or Ctrl/Cmd + Shift + I)
- Go to the Network tab
- Click on "Library" in YouTube Music to trigger a request
- Filter by browse
- Right-click on a POST request under the name column
- Select Copy Value → Copy 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();
Cookie Method
For Chrome users or when the browser method doesn't work, you can use cookies directly.
Extract Cookies
- Go to https://music.youtube.com in your browser
- Open Developer Tools → Network tab
- Find a request to a browse or next endpoint
- Click on the request and go to the Headers tab
- Copy the Cookie value
- 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();
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";
}