gpt4 book ai didi

ios - iOS 上的推特 OAuth

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:11 25 4
gpt4 key购买 nike

我需要通过 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 上的推特 OAuth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39036618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com