Google Sign in server auth code nil iOS?

不问归期 提交于 2020-01-15 07:34:11

问题


We have Google sign in our application.We are providing serverClientID at the time of request of login.

We are getting user.serverAuthCode as nil.

Our request is like below :

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

        }

We are trying to get the serverAuthCode as follows :

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

    }

But its serverAuthCode is null we are not sure what may have gone wrong.


回答1:


The catch is that the server auth code will only be sent the first time the user logs in. The user must have just seen the confirmation screen. On all subsequent logins, no server auth code will be sent. Go to https://security.google.com/settings/security/permissions and removed the credentials for your app, then you'll get a server auth code.




回答2:


GIDSignInDelegate ,GIDSignInUIDelegate use 2 delegates.

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()

Present a view that prompts the user to sign in with Google

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

Dismiss the "Sign in with Google" view

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

completed sign In

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

Don't forgot this to add in info.plist file

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



回答3:


I had similar problem with Google login in my app also.

Here is the solution that worked for me.

Swift 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()
}



回答4:


you must set clientId and server client Id in the app delegate in the didFinishLaunchingWithOptions

GIDSignIn.sharedInstance().clientID = ""

GIDSignIn.sharedInstance().serverClientID = ""



来源:https://stackoverflow.com/questions/42434887/google-sign-in-server-auth-code-nil-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!