Swift 3错误403中的API YouTube



我试图在iOS Swift中使用YouTube API,并遵循本教程http://www.appcoda.com/youtube-apioios-tutorial/HTTP状态代码= 403加载频道详细信息时错误:nil

我正在使用Swift 3

var urlString = "https://www.googleapis.com/youtube/v3/search?part=snippet&q=(textField.text)&type=(type)&key=(apiKey)"
urlString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
// Create a NSURL object based on the above string.
let targetURL = URL(string: urlString)
// Get the results.
performGetRequest(targetURL, completion: { (data, HTTPStatusCode, error) -> Void in
    if HTTPStatusCode == 200 && error == nil {
        // Convert the JSON data to a dictionary object.
        do {
            let resultsDict = try JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<String, AnyObject>
            // Get all search result items ("items" array).
            let items: Array<Dictionary<String, AnyObject>> = resultsDict["items"] as! Array<Dictionary<String, AnyObject>>
            // Loop through all search results and keep just the necessary data.
            for i in 0 ..< items.count {
                let snippetDict = items[i]["snippet"] as! Dictionary<String, AnyObject>
                // Gather the proper data depending on whether we're searching for channels or for videos.
                if self.segDisplayedContent.selectedSegmentIndex == 0 {
                    // Keep the channel ID.
                    self.desiredChannelsArray.append(snippetDict["channelId"] as! String)
                }
                else {
                    // Create a new dictionary to store the video details.
                    var videoDetailsDict = Dictionary<String, AnyObject>()
                    videoDetailsDict["title"] = snippetDict["title"]
                    videoDetailsDict["thumbnail"] = ((snippetDict["thumbnails"] as! Dictionary<String, AnyObject>)["default"] as! Dictionary<String, AnyObject>)["url"]
                    videoDetailsDict["videoID"] = (items[i]["id"] as! Dictionary<String, AnyObject>)["videoId"]
                    // Append the desiredPlaylistItemDataDict dictionary to the videos array.
                    self.videosArray.append(videoDetailsDict)
                    // Reload the tableview.
                    self.tblVideos.reloadData()
                }
            }
        } catch {
            print(error)
        }
        // Call the getChannelDetails(…) function to fetch the channels.
        if self.segDisplayedContent.selectedSegmentIndex == 0 {
            self.getChannelDetails(true)
        }
    }
    else {
        print("HTTP Status Code = (HTTPStatusCode)")
        print("Error while loading channel videos: (error)")
    }
    // Hide the activity indicator.
    self.viewWait.isHidden = true
})

return true

}

// MARK: Custom method implementation
func performGetRequest(_ targetURL: URL!, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int, _ error: NSError?) -> Void) {
   // let request = NSMutableURLRequest(url: targetURL)
   // request.httpMethod = "GET"
    var request = URLRequest(url: targetURL)
    request.httpMethod = "GET"
    let sessionConfiguration = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfiguration)
  /*  let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        DispatchQueue.main.async(execute: { () -> Void in
            completion(data, (response as! HTTPURLResponse).statusCode, error)
        })
    } as! (Data?, URLResponse?, Error?) -> Void)*/

  /*  let task = session.dataTask(with: request, completionHandler: ({ (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        DispatchQueue.main.async(execute: { () -> Void in
            completion(data as Data?, (response as! HTTPURLResponse).statusCode, error)
        })
        } as! (Data?, URLResponse?, Error?) -> Void))*/
  let task = session.dataTask(with: request) { data, response, error in DispatchQueue.main.async { completion(data, (response as! HTTPURLResponse).statusCode, error as? NSError) } }
    task.resume()
}
  • 首先,Swift 3中的所有JSON字典表示形式是[String:Any](aka Dictionary<String,Any>

  • swift 3中的所有参数标签已删除

    func performGetRequest(_ targetURL: URL, completion: @escaping (Data?, Int, NSError?) -> Void) {
    

    不要将隐式未包装的选项用于方法参数类型。使用常规可选(?)或非选项。

错误403表示禁止访问。确保您有正确的 Apikey 来自Google/YouTube开发人员。

我还使用了Appcoda YouTube API教程(我认为这是Swift 2),这是Swift 3的工作版本。

    func getVideosForChannelAtIndex() {

    let urlString = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=(playlistID)&maxResults=(maxResults)&key=(apiKey)"
    // Create a NSURL object based on the above string.
    let targetURL = URL(string: urlString)
    // Fetch the playlist from Google.
    performGetRequest(targetURL!) { (data, HTTPStatusCode, error) -> Void in

        if HTTPStatusCode == 200 && error == nil {
            do {
                self.videos = []
                // Convert the JSON data into a dictionary.
                let resultsDict = try JSONSerialization.jsonObject(with: data!, options: []) as! Dictionary<AnyHashable, Any>
                // Get all playlist items ("items" array).
                let items:Array<Dictionary<AnyHashable, Any>> = resultsDict["items"] as! Array<Dictionary<AnyHashable, Any>>
                // Use a loop to go through all video items.
                //                    for var i=0; i<items.count; ++i
                for i in 0 ..< items.count {
                    let playlistSnippetDict = (items[i] as Dictionary<AnyHashable, Any>)["snippet"] as! Dictionary<AnyHashable, Any>
                    let video = Video()
                    video.title = playlistSnippetDict["title"] as? String
                    //                        video.thumbnail =
                    video.videoId = (playlistSnippetDict["resourceId"] as? Dictionary<AnyHashable, Any>)?["videoId"] as? String
                    guard let thumbnail = ((playlistSnippetDict["thumbnails"] as? Dictionary<AnyHashable, Any>)?["high"] as? Dictionary<AnyHashable, Any>)?["url"] as? String else {
                        video.thumbnail = UIImage(named: "Icon1024x1024")
                        return
                    }
                    guard let url:URL? = URL(string: thumbnail), let data:Data? = try? Data(contentsOf: url!) else {
                        video.thumbnail = UIImage(named: "Icon1024x1024")
                        return
                    }
                    if let dataImage = data {
                        video.thumbnail = UIImage(data: dataImage)
                    } else {
                        video.thumbnail = UIImage(named: "Icon1024x1024")
                    }
                    self.videos.append(video)
                    // Reload the tableview.
                    self.tblVideos.reloadData()
                }
            } catch {
                print("json error: (error)")
            }
        } else {
            print("")
            print("HTTP Status Code = (HTTPStatusCode)")
            print("")
            //Show alertDialog here with Error
            print("Error while loading videos: (error?.localizedDescription)")
            let alert = UIAlertView(title: "Oops!", message: error?.localizedDescription, delegate: self, cancelButtonTitle: "OK")
            alert.show()
        }
        // Hide the activity indicator.
        self.viewWait.isHidden =  true
    }
}

这是针对PerformgetRequest

func performGetRequest(_ targetURL: URL, completion: @escaping (_ data: Data?, _ HTTPStatusCode: Int?, _ error: Error?) -> Void) {
    var request = URLRequest(url: targetURL)
    request.httpMethod = "GET"
    let sessionConfiguration = URLSessionConfiguration.default
    let session = URLSession(configuration: sessionConfiguration)
    let task = session.dataTask(with: request) { data, response, error in
        DispatchQueue.main.async(execute: {
            completion(data, (response as? HTTPURLResponse)?.statusCode, error)
        })
    }
    task.resume()
}

最新更新