2017-02-24 66 views
1

我們已經Google登錄我們的應用程序。我們在請求登錄時提供serverClientID。谷歌登錄服務器驗證碼無iOS?

我們得到user.serverAuthCode爲nil。

我們的要求是象下面這樣:

func googleLogin(){ 
      var configureError: NSError? 
      GGLContext.sharedInstance().configureWithError(&configureError) 
      assert(configureError == nil, "Error configuring Google services: \(configureError)") 

      let gid = GIDSignIn.sharedInstance() 
      if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") { 
       let myDict = NSDictionary(contentsOfFile: path) 
       gid?.serverClientID = "our servers cliet id as configured over firebase" 

// This client id of our ios app we are getting from 
// GoogleService-info.plist 
       gid?.clientID = myDict!.value(forKey: "CLIENT_ID") as! String 

      } 
      //  gid?.serverClientID = "our_servers" // TODO: fetch from plist 
      gid?.scopes.append("https://www.googleapis.com/auth/gmail.readonly") 
      print("\nClient Id: \(gid!.clientID!) Server Client Id: \(gid!.serverClientID!)\n") 
      gid?.delegate = self 

     } 

我們正在試圖讓serverAuthCode如下:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 
     if (error == nil) { 
      // Perform any operations on signed in user here. 
      let auth = user.serverAuthCode 
      print(auth) 
      let fullName = user.profile.name 
     } else { 
      print("\(error.localizedDescription)") 
     } 

    } 

但其serverAuthCode爲空,我們不知道什麼可能已經錯了。

+0

您的問題解決了沒有? –

回答

4

GIDSignInDelegate ,GIDSignInUIDelegate使用2個代表。

let signin : GIDSignIn = GIDSignIn .sharedInstance() 
signin.shouldFetchBasicProfile = true; 
signin.uiDelegate = self 
signin.delegate = self 
GIDSignIn.sharedInstance().signInSilently() // this for swift 3.0 
GIDSignIn.sharedInstance().signIn() 

目前,提示用戶與谷歌

func sign(_ signIn: GIDSignIn!, 
      present viewController: UIViewController!) { 
    self.present(viewController, animated: true, completion: nil) 
} 

登錄視圖駁回 「在與谷歌」 的看法

func sign(_ signIn: GIDSignIn!, 
      dismiss viewController: UIViewController!) { 
    self.dismiss(animated: true, completion: nil) 
} 

完成登錄

public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 

     if (error == nil) { 
      // Perform any operations on signed in user here. 
      let userId = user.userID     // For client-side use only! 
      let idToken = user.authentication.idToken // Safe to send to the server 
      let fullName = user.profile.name 
      let givenName = user.profile.givenName 
      let familyName = user.profile.familyName 
      let email = user.profile.email 
      let imageurl = user.profile.imageURL(withDimension: 1080) 
      print("Gmail id %@" ,userId!) 
      print("Gmail token %@" ,idToken!) 
      print("Gmail full name %@" ,fullName!) 
      print("Gmail first name %@" ,givenName!) 
      print("Gmail last name %@" ,familyName!) 
      print("Gmail emailid %@" ,email!) 

      print("Gmail id %@" ,imageurl!) 

     } else { 
      print("\(error.localizedDescription)") 
     } 
    } 

不要忘記這在Info.plist文件中添加

<dict> 
     <key>CFBundleTypeRole</key> 
     <string>Editor</string> 
     <key>CFBundleURLSchemes</key> 
     <array> 
      <string>com.googleusercontent.apps.************************</string> 
     </array> 
    </dict> 
0

我在我的應用程序有類似的問題與谷歌登錄也。

這是爲我工作的解決方案。

斯威夫特3: -

let serverAuth = user.serverAuthCode 

if serverAuth != nil { 
    //Server auth is received successfully so you can user navigation. 
} 
else{ 
    //Here we make the user logout programatically. So now onwards if user clicks log in with google we won't get nil serAuthCode 
    GIDSignIn.sharedInstance().signOut() 
}