Skip to content

Enchan1207/YoutubeKit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YoutubeKit

SPM platforms release

Overview

Swift framework for Youtube Data API (v3).

Installation

Now, This framework only supports SPM (Swift Package Manager).

Usage

NOTE: Before you read, please create project and generate API credentials on Google Cloud Platform.

Instantiation

If you don't have any access tokens:

// Credentials
let API_KEY = "XXXXXX"
let CLIENT_ID = "XXXXXX"
let CLIENT_SECRET = "XXXXXX"

// instantiation 
let API_CREDENTIAL = YoutubeKit.APICredential(apikey: API_KEY, clientID: CLIENT_ID, clientSecret: CLIENT_SECRET)

let youtube = YoutubeKit(apiCredential: API_CREDENTIAL, accessCredential: nil)

or if you already have access token:

// Credentials
let API_KEY = "XXXXXX"
let CLIENT_ID = "XXXXXX"
let CLIENT_SECRET = "XXXXXX"
let ACCESS_TOKEN = "XXXXXX"
let REFRESH_TOKEN = "XXXXXX"

// instantiation 
let API_CREDENTIAL = YoutubeKit.APICredential(apikey: API_KEY, clientID: CLIENT_ID, clientSecret: CLIENT_SECRET)
let ACCESS_CREDENTIAL = YoutubeKit.AccessCredential(accessToken: ACCESS_TOKEN, refreshToken: REFRESH_TOKEN, expires: Date(), grantedScopes: [.readwrite])

let youtube = YoutubeKit(apiCredential: API_CREDENTIAL, accessCredential: ACCESS_CREDENTIAL)

Authorize

To authorize to access user datas in your application, set Scope and call authorize.
(in details about scope, see here).

NOTE: This authorization flow can be only used at iOS or macOS. if you want to use it at console application, you need to set AccessCredential when instantiation.

for iOS:

let scope: [YoutubeKit.Scope] = [.readwrite, .forceSSL]
self.youtube.authorize(presentViewController: self, scope: scope) { (credential) in
    print(credential)
} failure: { (error) in
    print(error)
}

for macOS:

let scope: [YoutubeKit.Scope] = [.readwrite, .forceSSL]
self.youtube.authorize(scope: scope) { (credential) in
    print(credential)
} failure: { (error) in
    print(error)
}

Call API endpoints

Example: search

let query = "HIKAKIN"
self.youtube.search(query: query, maxResults: 1) { (result) in
    for item in result.items{
        print(item.serialize()!)
    }
} failure: { (_error) in
    print(error)
}

Response:

{
    "id": {
        "kind": "youtube#channel",
        "channelId": "UCZf__ehlCEBPop-_sldpBUQ"
    },
    "snippet": {
        "thumbnails": {
            "default": {
                "url": "https:\/\/yt3.ggpht.com\/ytc\/AAUvwniFFE8I4vqAWJY-iQltV1kvYhtD-iM0wgsS6nv9lA=s88-c-k-c0xffffffff-no-rj-mo"
            },
            "high": {
                "url": "https:\/\/yt3.ggpht.com\/ytc\/AAUvwniFFE8I4vqAWJY-iQltV1kvYhtD-iM0wgsS6nv9lA=s800-c-k-c0xffffffff-no-rj-mo"
            },
            "medium": {
                "url": "https:\/\/yt3.ggpht.com\/ytc\/AAUvwniFFE8I4vqAWJY-iQltV1kvYhtD-iM0wgsS6nv9lA=s240-c-k-c0xffffffff-no-rj-mo"
            }
        },
        "channelTitle": "HikakinTV",
        "title": "HikakinTV",
        "description": "HikakinTVはヒカキンが日常の面白いものを紹介するチャンネルです。 ◇プロフィール◇ YouTubeにてHIKAKIN、HikakinTV、HikakinGames、HikakinBlogと 4つの ...",
        "channelId": "UCZf__ehlCEBPop-_sldpBUQ",
        "publishedAt": "2011-07-19T11:31:43Z"
    }
}

Dependencies

Due to the update timing, it is possible that everything has not been listed here. See Package.swift for the exact dependencies.