Google登录服务器身份验证代码是否没有iOS?

San*_*pta 1 google-api ios google-oauth swift gmail-api

我们已经在应用程序中使用Google登录了。我们在请求登录时提供了serverClientID。

我们将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

        }
Run Code Online (Sandbox Code Playgroud)

我们试图得到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)")
        }

    }
Run Code Online (Sandbox Code Playgroud)

但是它serverAuthCode是无效的,我们不确定可能出了什么问题。

Him*_*iya 5

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()
Run Code Online (Sandbox Code Playgroud)

显示一个视图,提示用户使用Google登录

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

取消“使用Google登录”视图

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

完成登录

    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)")
        }
    }
Run Code Online (Sandbox Code Playgroud)

不要忘记将此添加到info.plist文件中

  <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>com.googleusercontent.apps.************************</string>
        </array>
    </dict>
Run Code Online (Sandbox Code Playgroud)

  • 我不明白如何从这个答案中获得 authCode。 (2认同)

Pol*_*ill 5

问题在于,仅在用户首次登录时才发送服务器身份验证代码。用户必须已经看到确认屏幕。在所有后续登录中,将不发送服务器身份验证代码。转到https://security.google.com/settings/security/permissions并删除您的应用程序的凭据,然后您将获得服务器身份验证代码。