You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS端YouTube创建播放列表遇未授权错误求助

Hey there, let's troubleshoot that unauthorized error you're facing when creating a YouTube playlist in Swift. I've seen this exact issue a few times, so let's walk through the most likely fixes:

1. You Do Need Proper OAuth Credentials (Even Without a Hardcoded Client Secret)

First off—while you don't need to embed a client secret directly in your iOS app, you still have to set up OAuth credentials in the Google Cloud Console. Here's why:

  • Google's servers need to verify your app's identity to grant access. Without configuring an iOS OAuth Client ID (matching your app's bundle ID), your authorization requests will be rejected outright.
  • Fix steps:
    • Go to the Google Cloud Console, select your project.
    • Navigate to APIs & Services > Credentials.
    • Create an iOS OAuth client ID, enter your app's bundle ID exactly as it appears in Xcode.
    • Download the generated GoogleService-Info.plist file and add it to your Xcode project (make sure it's included in your app target).
2. Your Authorization Scope Might Be Wrong or Overly Broad

You mentioned using kGTLRAuthScopeYouTubeYoutubepartner—that's for YouTube Partner Program-specific features (like monetization management), which is way more than you need for creating a basic playlist. Using this scope could cause issues if your account isn't part of the Partner Program, or if the user hasn't granted those elevated permissions.

  • The correct scopes for playlist creation are:
    • kGTLRAuthScopeYouTube: Grants full access (including write permissions) – fine for testing, but for production, use more specific scopes.
    • kGTLRAuthScopeYouTubePlaylistModifyPublic: For creating public playlists.
    • kGTLRAuthScopeYouTubePlaylistModifyPrivate: For creating private/unlisted playlists.
  • Fix: Strip out the kGTLRAuthScopeYouTubeYoutubepartner scope and use only the ones you actually need. Make sure you request these scopes when initiating the Google Sign-In flow.
3. Double-Check Your Authorization Flow & Playlist Creation Logic

Even if you think your logic is correct, let's rule out common gotchas:

  • Ensure you're attaching the authenticated authorizer to your GTLRYouTubeService instance every time you make a request. A common mistake is reinitializing the service without setting the authorizer:
    // Correct approach
    let service = GTLRYouTubeService()
    guard let authorizer = GIDSignIn.sharedInstance.currentUser?.authentication.fetcherAuthorizer() else {
        print("User not authenticated!")
        return
    }
    service.authorizer = authorizer
    
  • Verify your playlist creation code includes all required fields. Here's a minimal, correct example of what your createYoutubePlaylistWithTitle function should look like:
    func createYoutubePlaylistWithTitle(_ title: String, description: String? = nil, isPublic: Bool = false) {
        // Create playlist snippet with title/description
        let snippet = GTLRYouTube_PlaylistSnippet()
        snippet.title = title
        snippet.description = description ?? "Created via my iOS app"
        
        // Set privacy status
        let status = GTLRYouTube_PlaylistStatus()
        status.privacyStatus = isPublic ? "public" : "private"
        
        // Assemble the playlist object
        let playlist = GTLRYouTube_Playlist()
        playlist.snippet = snippet
        playlist.status = status
        
        // Build the insert query (must include "snippet,status" in the part parameter)
        let query = GTLRYouTubeQuery_PlaylistsInsert.query(withObject: playlist, part: "snippet,status")
        
        // Configure service with authorizer
        let service = GTLRYouTubeService()
        guard let authorizer = GIDSignIn.sharedInstance.currentUser?.authentication.fetcherAuthorizer() else {
            print("No authenticated user found!")
            return
        }
        service.authorizer = authorizer
        
        // Execute the request
        service.executeQuery(query) { ticket, response, error in
            if let error = error {
                print("Playlist creation failed: \(error.localizedDescription)")
                return
            }
            guard let createdPlaylist = response as? GTLRYouTube_Playlist else { return }
            print("Success! Playlist ID: \(createdPlaylist.id ?? "")")
        }
    }
    
  • Make sure the user has actually signed in and granted the required permissions before calling this function. If the authorization token is expired or missing, you'll get an unauthorized error.
4. Don't Forget to Enable the YouTube Data API

One last thing—ensure the YouTube Data API v3 is enabled in your Google Cloud Console project. If it's not enabled, even properly authenticated requests will fail with an unauthorized or "API not enabled" error.

  • To enable it: Go to APIs & Services > Library, search for "YouTube Data API v3", and click "Enable".

Give these steps a try—chances are one of them will fix your unauthorized error.

内容的提问来源于stack exchange,提问作者cluelessatmaths

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:16:13