我需要通过 twitter 在我们的服务器上注册。我在 OAuth 的帮助下进行了注册。当我们在我们的服务器上发出任何请求时,我们会检查我们的 token 是否处于事件状态,如果我们的 token 已过期,我们会在我们的服务器上辞职,我需要在 Twitter session 中退出,我会注销,但 session 是事件的。 Android 团队在帮助下做到这一点
TwitterCore.getInstance().getSessionManager().clearActiveSession();
TwitterCore.getInstance().logOut();
。我怎样才能在 iOS 上做到这一点?我使用 Swift 请帮帮我。
我的代码
func auth(userEmail: String?, successLoginCallback: ((usr : User) -> Void)?, failLoginCallback: ( (message: String) -> Void )?) {
Twitter.sharedInstance().sessionStore.reloadSessionStore()
Twitter.sharedInstance().startWithConsumerKey(twitterKey, consumerSecret: twitterSecret)
Twitter.sharedInstance().logInWithCompletion { (session, error) in
if session != nil {
TWTRAPIClient.clientWithCurrentUser().requestEmailForCurrentUser({ (twitterEmail, error) in
// oauth parameters
let oauthSession = TWTRSession(authToken: session!.authToken, authTokenSecret: session!.authTokenSecret, userName: session!.userName, userID: session!.userID)
let oauthSigning = TWTROAuthSigning(authConfig: Twitter.sharedInstance().authConfig, authSession: oauthSession)
let headers = oauthSigning.OAuthEchoHeadersToVerifyCredentials()
guard let authHeaders = headers as? [String : AnyObject] else {
return
}
var passEmail: String?
if userEmail != nil {
passEmail = userEmail
}
UserManager.sharedManager.logInViaTwitter(passEmail, parameters: authHeaders, success: { (user) in
self.userIsLogginedViaTwitter = true
self.twitterSuccessLogin(user)
print("user token", user.token?.expr)
successLoginCallback?(usr: user)
}, fail: { (errorMessage) in
if errorMessage != nil {
self.twitterFailsLogin(errorMessage!)
failLoginCallback?(message: errorMessage!)
}
})
})
}
if error != nil {
print(error?.localizedDescription)
}
}
}
func logOut(success: (() -> ())?) {
Twitter.sharedInstance().startWithConsumerKey(twitterKey, consumerSecret: twitterSecret)
guard let userID = Twitter.sharedInstance().sessionStore.session()?.userID else { return }
Twitter.sharedInstance().sessionStore.logOutUserID(userID)
userIsLogginedViaTwitter = false
let cookieStorage: NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage()
cookieStorage.cookies?.forEach({ (cook) in
cookieStorage.deleteCookie(cook)
})
success?()
}
// MARK: - Twitter functions
func logInViaTwitter(email: String?, parameters: [String : AnyObject], success: ((user: User) -> ())?, fail: ((errorMessage: String?) -> ())?) {
let url = "\(Networking.baseUrl)/auth/login/tw"
guard let oauth_url = parameters["X-Auth-Service-Provider"] as? String else { return }
guard let oauth_params = parameters["X-Verify-Credentials-Authorization"] as? String else { return }
var passParameters = [String : AnyObject]()
if email != nil {
passParameters = ["email" : email!, "oauth_url" : oauth_url, "oauth_params": oauth_params]
} else {
passParameters = ["oauth_url" : oauth_url, "oauth_params": oauth_params]
}
print("Twitter manager passParameters", passParameters, "Twitter manager passParameters")
Networking.manager.request(.POST, url, parameters: passParameters, encoding: .JSON, headers: nil).responseObject { (response: Response<RegisterResponse, NSError>) in
guard let result = response.result.value else {
fail?(errorMessage: nil)
return
}
if result.meta?.errors.count == 0 {
print(result.response)
guard let user = Mapper<User>().map(result.response) else {
fail?(errorMessage: nil)
return
}
// Save state in Twitter manager
TwitterManager.sharedManager.userIsLogginedViaTwitter = true
self.saveCurrentUser(user)
success?(user: user)
} else {
fail?(errorMessage: result.meta?.errors.first?.message)
}
}
}
我们更改了查询逻辑,因此对于每个请求,我们都不会从 Twitter 框架中请求信息。
关于ios - iOS 上的 Twitter OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036618/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |