2016-12-09 6 views
0

Я пытаюсь использовать API в юности Ios быстрых, и после этого урока http://www.appcoda.com/youtube-api-ios-tutorial/ HTTP Status Code = 403 Ошибки при загрузке сведений о канале: нольAPI YouTube в быстрой 3 ошибке 403

Я использую быстр 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() 

} 

ответ

0
  • Прежде всего словарного представления JSON в Swift 3 является [String:Any] (ака Dictionary<String,Any>)

  • Во-вторых, в Swift 3 все метки параметров в затворов были удалены

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

    Не используйте неявные распакованных УСТРОЙСТВА для типов параметров метода. Либо используйте обычный необязательный (?), либо необязательный.

0

вы в то время как изменение функ performGetRequest (_ targetURL: URL, завершение: @escaping (Data ?, Int, NSError) -> Пустота) { с URLSession.shared.dataTask

0

Ошибка 403 означает Запретный доступ. Убедитесь, что у вас есть apiKey от разработчика google/youtube.

Я также использовал appcoda использования API YouTube учебник (который в Swift 2 я думаю), и это рабочая версия шахты для быстрой 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() 
} 
Смежные вопросы