Search code examples
iosswiftgidsignin

Unable to get Authcode after google SignIn in iOS (swift)


Problem: In iOS app I am using google signIn to facilitate the process of getting the authCode, which later is sent into the server and it is exchanged for a refresh_token in google calendar API. The bad news is that user.serverAuthCode is always nil.

What happens now: I have followed the google's documentation for implementing it. At this moment when I click the google sign-in button I go to a page where I fill in my credentials and I consent to all the scopes. Then it redirects me back to iOS app but I don't get the authCode.

Code:

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate{
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
            print("Authcode is : \(user.serverAuthCode)") // This is nil :(
        }
    }


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        GIDSignIn.sharedInstance().clientID = "MY_CLIENT_ID"
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar")     
        GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar.events")

        return true
    }

 }

I've also added URL type in target info as described in google's documentation

Lastly I have a viewController which uses GIDSignInButton to start the log-in process.


Is there anything I am missing? I took a look at this post but I could not solve the problem. Thank you for your time!


Solution

  • After some time spending on searching the solution was found :)

    Problem fixed after adding:

    GIDSignIn.sharedInstance()?.serverClientID = "SERVER_CLIENT"
    

    If you don't know how to get these: SERVER_CLIENT vs CLIENT_ID