0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 627 forks source link

XCDYouTubeKit + YouTube authentication? #378

Closed XRadevFRT closed 4 years ago

XRadevFRT commented 6 years ago

Greeting every on and great job 0xced,

I'm just wondering...can this library co-work with the youtube authentication. Of course the idea is to fetch and play your videos after you log in in your youtube account. I haven't implemented it yet, but just wondering.

ogezue commented 6 years ago

Having the same problem loading the captions https://developers.google.com/youtube/v3/docs/captions/download. This requires a login.

As XCDYouTubeKit contains + (instancetype)queryForCaptionsDownloadWithIdentifier:(NSString *)identifier;

the must be a way to authenticate...

XRadevFRT commented 6 years ago

You mean the identifier is the user ID or the video ID... If we get a list of videos, aren't the IDs of the videos contained in the response? Can't we just use them to init the XCDYouTube?

ogezue commented 6 years ago

Identifier is the identifier of the caption.

Here is how I retrieve the captions list, select the English one an try to download it:

func fetchCaptions(videoId: String?, successHandler: @escaping () -> Void, errorHandler: @escaping () -> Void){

        guard let videoId = videoId else {
            return
        }

        let query = GTLQueryYouTube.queryForCaptionsList(withPart: "snippet", videoId: videoId)

        _ = youTubeService?.executeQuery(query!, completionHandler: {[weak self] (ticket, response, error) in
            if let response = response as? GTLYouTubeCaptionListResponse{

                guard let items = response.items() else {
                    logger.error("error retrieving captions - no caption items received" )
                    errorHandler()
                    return
                }

                for item in items{
                    if let caption = item as? GTLYouTubeCaption{
                        print(caption)
                        if caption.snippet.language == "en"{
                            self?.fetchCaptionWithId(captionId: caption.identifier, videoId: videoId, successHandler: {
                                successHandler()
                            }, errorHandler: {
                                errorHandler()
                            })
                        }
                    }
                }

            }else{
                logger.error(error?.localizedDescription ?? "- no youtube captions received" )
                errorHandler()
            }
        })
    }

    func fetchCaptionWithId(captionId: String, videoId: String,
                            successHandler: @escaping () -> Void, errorHandler: @escaping () -> Void){

        let query = GTLQueryYouTube.queryForCaptionsDownload(withIdentifier: captionId)
        query?.tfmt = "srt"
        query?.videoId = videoId

        _ = youTubeService?.executeQuery(query!, completionHandler: {(ticket, response, error) in

            if let response = response as? GTLYouTubeCaptionListResponse{
                print(response)
            }else{
                logger.error(error?.localizedDescription ?? "- no youtube caption received" )
                errorHandler()
            }
        })
    }