列表需要3个元素,但我不知道第三个元素来自哪里

时间:2016-02-07 22:30:28

标签: swift oauth-2.0

所以我试图从here开始关注oAuth 2.0的教程。 在" OAuthSwift with Embedded Web View"部分下。 这是整个功能:

// 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个参数但指定只有两个。任何帮助都会非常感激。

1 个答案:

答案 0 :(得分: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
}