2016-02-07 22 views
0

所以我試圖從here跟隨oAuth 2.0的教程。 在「帶有嵌入式Web視圖的OAuthSwift」部分下。 這是整個功能:列表期望3個元素,但我不知道第三個來自哪裏

// 1 Create OAuth2Swift object 
let oauthswift = OAuth2Swift(
    consumerKey: "YOUR_GOOGLE_DRIVE_CLIENT_ID",   // 2 Enter google app settings 
    consumerSecret: "YOUR_GOOGLE_DRIVE_CLIENT_SECRET", 
    authorizeUrl: "https://accounts.google.com/o/oauth2/auth", 
    accessTokenUrl: "https://accounts.google.com/o/oauth2/token", 
    responseType: "code" 
) 
// 3 Trigger OAuth2 dance 
oauthswift.authorizeWithCallbackURL(
    NSURL(string: "com.raywenderlich.Incognito:/oauth2Callback")!, 
    scope: "https://www.googleapis.com/auth/drive",  // 4 Scope 
    state: "", 
    success: { credential, response in 
    var parameters = [String: AnyObject]() 
    // 5 Get the embedded http layer and upload 
    oauthswift.client.postImage(
     "https://www.googleapis.com/upload/drive/v2/files", 
     parameters: parameters, 
     image: self.snapshot(), 
     success: { data, response in 
     let jsonDict: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, 
      options: nil, 
      error: nil) 
     self.presentAlert("Success", message: "Successfully uploaded!") 
     }, failure: {(error:NSError!) -> Void in 
     self.presentAlert("Error", message: error!.localizedDescription) 
    }) 
    }, failure: {(error:NSError!) -> Void in 
    self.presentAlert("Error", message: error!.localizedDescription) 
}) 

我得到4點(範圍)的錯誤的列表填充:

success: { credential, response in 
    var parameters = [String: AnyObject]() 

它說,預計3個參數,但指定只是兩個。任何幫助將非常感激。

回答

0

這可能是兩個參數在教程製作(或上次更新)時有效,現在它需要三個參數。因此,更換:

success: { credential, response in 
    var parameters = [String: AnyObject]() 
    ... 

有:

success: { credential, response, parameters in 
    ... 

let jsonDict線也遵循舊格式,並且不處理錯誤,所以將其更改爲:

do { 
    let jsonDict = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String: AnyObject] 
    // And replace [String: AnyObject] with your JSON format. 
} catch { 
    // Error handling here 
} 
+0

所以,基本上我必須刪除整行'var parameters = [String:AnyObject]()'?並添加'參數'作爲第三個預期的參數?當我這樣做時,我在'let jsonDict:AnyObject! = NSJSONSerialization.JSONObjectWithData(data,options:nil,error:nil)'which is:「Extra argument error in call」。 – Walker

+0

是的,並看到我更新的答案。 – xoudini

+0

謝謝,但現在有錯誤處理的問題。我必須嘗試並抓住... – Walker

相關問題