2015-09-26 51 views
0

我在一個應用程序的工作中使用谷歌加登錄
它工作正常,但我想與解析整合也
refer例如如何獲取會話令牌谷歌+登錄與解析在IOS swift2

func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) { 
    let token = auth.accessToken    
    PFUser.becomeInBackground(token, block: { (user : PFUser?, error : NSError?) -> Void in 
     if error != nil { 
      print("Error in become user : \(error)") 
     } else { 
      print("user : \(user)") 
     } 
    }) 
} 

,但它給我像

Error in become user : Optional(Error Domain=Parse Code=209 "invalid session token" UserInfo={code=209, temporary=0, error=invalid session token, NSLocalizedDescription=invalid session token}) 

錯誤我也嘗試這個例子Login with google plus in ios on parse.com
但它在客觀C,嘗試轉換到swift2,但它也給錯誤

請給我正確的解決方案

回答

1

最後解決的問題
轉換成answer SWIFT代碼和它的做工精細

func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) { 
    if error == nil && auth.expirationDate.compare(NSDate(timeIntervalSinceNow: 0)) == NSComparisonResult.OrderedDescending { 

     let user = GPPSignIn.sharedInstance().googlePlusUser 
     let userName = user.name.JSONValueForKey("givenName") as! String 
     let userEmail = GPPSignIn.sharedInstance().userEmail 

     let pfUser = PFUser() 
     pfUser.username = userName 
     pfUser.email = userEmail 

     let userPassword = "\(userName)@123" 
     pfUser.password = userPassword 

     pfUser.signUpInBackgroundWithBlock({ (success, error : NSError?) -> Void in 

      if error == nil { 

      let plusService = GTLServicePlus() 
      plusService.retryEnabled = true 
      plusService.authorizer = GPPSignIn.sharedInstance().authentication 

      let query : GTLQueryPlus! = GTLQueryPlus.queryForPeopleGetWithUserId("me") as! GTLQueryPlus 

      plusService.executeQuery(query, completionHandler: { (ticket, person, error) -> Void in 
       if error != nil { 
        print("Error in execute query : \(error!)") 
       } else { 
        let aPerson : GTLPlusPerson! = person as! GTLPlusPerson 
        let imgUrl = aPerson.image.url 

        if let imgData = NSData(contentsOfURL: NSURL(string: imgUrl)!) { 
         self.userProfilePic.image = UIImage(data: imgData) 
        } 

        let currentUser = PFUser.currentUser() 
        currentUser?.username = aPerson.displayName 
        currentUser?.saveInBackground() 
       } 
       }) 

      } else { 

       print("Error in signup : \(error!.localizedDescription)") 

       PFUser.logInWithUsernameInBackground(self.userName, password: userPassword, block: { (user : PFUser?, error : NSError?) -> Void in 
       if error == nil { 
          print("Login Sccessfully") 
       } else { 
          print("Error in login : \(error!.localizedDescription)") 
         } 
       }) 
      } 
     }) 
     } else { 
      print("Error in authentication : \(error.localizedDescription)") 
     } 
    } 

希望幫助的人!

0

我不認爲這是不正確的翻譯斯威夫特的問題(如生成錯誤通過在運行時期間解析,而不是Swift編譯器或Swift運行時)。嘗試使用「PFUser.enableRevocableSessionInBackground()」。欲瞭解更多詳情,請訪問https://parse.com/tutorials/session-migration-tutorial。希望它能幫助你。乾杯。

相關問題