                 
                 }
                 
-                let valid_through = requestDate.addingTimeInterval(TimeInterval.init(self.token["expires_in"] as! Int))
-                
-                self.token.setValue(valid_through, forKey: "valid_through") //using a Date to token validation keep it more simple
-                UserDefaults.standard.setValue(self.token.value(forKey: "valid_through"), forKey: "valid_through")
-
+                if let expiresIn = self.token["expires_in"] as? Int {
+                    let validThrough = requestDate.addingTimeInterval(TimeInterval.init(expiresIn))
+                    self.token.setValue(validThrough, forKey: "valid_through") //using a Date to token validation keep it more simple
+                    UserDefaults.standard.setValue(self.token.value(forKey: "valid_through"), forKey: "valid_through")
+                }
                 
                 NotificationCenter.default.post(name:.tokenDidAuthorize, object: JSONReturn, userInfo: nil)
 

    @objc func oAuthDidReturn(notification:Notification) -> Void {
        NotificationCenter.default.removeObserver(self, name: .oAuthDidReturn, object: nil)
        
        if(notification.object == nil){
            NotificationCenter.default.post(name:.oAuthDidFail, object: nil, userInfo: nil)
            return
        }
        
        let authQueryString = notification.object as! String
        
        let authParams = authQueryString.componentsFromQueryString
        
        if( authParams.count==0 || !authQueryString.contains(state) ){
            NotificationCenter.default.post(name:.oAuthDidFail, object: nil, userInfo: nil)
            return
        }

        getToken(with: "grant_type=authorization_code&code=\(code)&redirect_uri=\(self.redirect_uri)")
    }
    
    func getToken(with params:String) {
        
        let url = URL(string: "https://www.reddit.com/api/v1/access_token")!
        let session = URLSession.shared
