使用iOS客户端的Google端点:用户每次都必须登录

时间:2015-03-27 19:12:36

标签: ios google-oauth google-cloud-endpoints

我正在开发一个使用Google Endpoints API的iOS应用程序。要授权请求,用户必须在第一个屏幕上使用其Gmail帐户登录。我设法让这个工作,但问题是用户每次启动应用程序时都必须登录。有没有办法让会话持续一段时间?例如,当使用Facebook SDK for iOS时,一旦用户使用Facebook登录,会话将保持活动状态,直到用户明确注销。

谢谢,

1 个答案:

答案 0 :(得分:0)

我想出了我认为是解决这个问题的黑客行为。

第1步:用户使用他/她的Gmail帐户登录后,将身份验证对象属性保存到NSUserDefaults。

@objc(viewController:finishedWithAuth:error:)
    func finishedWithAuth(viewController :GTMOAuth2ViewControllerTouch , auth:GTMOAuth2Authentication,error:NSError!){

        self.dismissViewControllerAnimated(true, completion: nil);

        if error != nil {

            println("Authentication Failure: \(error.localizedDescription)");

        }else{

            GoogleEndpointAssistant.saveGTMOAuth2AuthenticationToUserDefaults(auth)

            setAuthentication(auth)

        }

GoogleEndpointAssistant.swift

class func saveGTMOAuth2AuthenticationToUserDefaults(auth : GTMOAuth2Authentication!){

            assert(auth.parameters != nil)
            assert(auth.parameters.count > 0)

            auth.parameters.setValue(auth.tokenURL.absoluteString, forKey: "token_url")
            auth.parameters.setValue(auth.redirectURI, forKey: "redirect_url")

            let defaults = NSUserDefaults.standardUserDefaults();
            defaults.setObject(auth.parameters, forKey: GMTOAuth2AuthenticationKey)

        }

第2步:在viewDidLoad中重建auth对象:

override func viewDidLoad() {

        if let auth : GTMOAuth2Authentication = GoogleEndpointAssistant.rebuildGTMOAuth2AuthenticationFromUserDefaults(){

            setAuthentication(auth)
        }

    }

GoogleEndpointAssistant.swift

class func rebuildGTMOAuth2AuthenticationFromUserDefaults()->GTMOAuth2Authentication?{

        let defaults = NSUserDefaults.standardUserDefaults();
        if let parameters  = defaults.dictionaryForKey(GMTOAuth2AuthenticationKey){

            let serviceProvider = parameters["serviceProvider"] as? String
            let tokenURL = NSURL(string:parameters["token_url"] as String)
            let redirectURL = parameters["redirect_url"] as String


            let auth : GTMOAuth2Authentication = GTMOAuth2Authentication.authenticationWithServiceProvider(
                serviceProvider,
                tokenURL: tokenURL,
                redirectURI: redirectURL,
                clientID: GoogleCloudEndPointClientID,
                clientSecret: GoogleCloudEndPointClientSecret) as GTMOAuth2Authentication

            auth.userEmail = parameters["email"] as String
            auth.userID = parameters["userID"] as String
            auth.userEmailIsVerified = parameters["isVerified"] as String
            auth.scope = parameters["scope"] as String
            auth.code = parameters["code"] as String
            auth.tokenType = parameters["token_type"] as String
            auth.expiresIn = (parameters["expires_in"] as NSNumber).longValue
            auth.refreshToken = parameters["refresh_token"] as String
            auth.accessToken = parameters["access_token"] as String

            return auth
        }
            return nil 
    }