2017-02-24 3 views
1

私たちのアプリケーションにはGoogleのサインインがあります。我々は、ログイン要求時にserverClientIDを提供しています。Googleログインサーバーの認証コードなし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は、私たちが間違っている可能性がわからないnullです。

+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) 
} 

"Googleアカウントでサインイン" ビューを閉じ促しビューを提示

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

を追加するのを忘れないでください。また、私は私のアプリでは、Googleのログインで同様の問題がありました。

私のために働いた解決策はここにあります。

スウィフト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() 
}