OAuthSwift / OAuthSwiftAlamofire

Utility method to sign Alamofire request
MIT License
104 stars 38 forks source link

Refresh token not firing automatically #11

Closed allistoncarlos closed 6 years ago

allistoncarlos commented 6 years ago

Do I need to write some extra code to refresh my access token?

I have the following (Swift 4) code and it's not refreshing...

let sessionManager = SessionManager.default sessionManager.adapter = OAuthSwiftRequestAdapter(AppDelegate.datalogOAuth) sessionManager.retrier = OAuthSwiftRequestAdapter(AppDelegate.datalogOAuth) as? RequestRetrier

Thanks in advance

phimage commented 6 years ago

OAuthSwiftRequestAdapter is for OAuth 1 OAuthSwift2RequestAdapter is for OAuth 2

There is no standards refresh token mechanism for OAuth1 Only OAuthSwift2RequestAdapter provide some code to refresh

But if you need you could implement your own RequestRetrier or adapt one of the RequestAdapter

allistoncarlos commented 6 years ago

Sorry, I forgot to mention that I was using OAuth2. I corrected the code, but still no automatically refresh on the token. Here is my updated code

let sessionManager = SessionManager.default sessionManager.adapter = OAuthSwift2RequestAdapter(AppDelegate.datalogOAuth) sessionManager.retrier = OAuthSwift2RequestAdapter(AppDelegate.datalogOAuth)

I think that I sill miss something...

phimage commented 6 years ago

any log or error?

there is a bug for some service #12

allistoncarlos commented 6 years ago

I've checked the workaround B and resolved my problem! I made an adaptation in order to save the token to the Keychain, and now is working great. Here is my code after that adaptation:

class OAuth2SwiftWorkaround: OAuth2Swift {
    override func renewAccessToken(withRefreshToken refreshToken: String, parameters: OAuthSwift.Parameters?, headers: OAuthSwift.Headers?, success: @escaping OAuthSwift.TokenSuccessHandler, failure: OAuthSwift.FailureHandler?) -> OAuthSwiftRequestHandle? {

        print("reset access_token")
        client.credential.oauthToken = ""
        client.credential.oauthTokenExpiresAt = nil

        return super.renewAccessToken(withRefreshToken: client.credential.oauthRefreshToken, parameters: parameters, headers: headers, success: { (credential, response, successParameters) in
            // Using Prephirences Pod
            KeychainPreferences.sharedInstance["OAuthToken"]         = credential.oauthToken
            KeychainPreferences.sharedInstance["OAuthRefreshToken"]  = credential.oauthRefreshToken

            success(credential, response, successParameters)
        }, failure: failure)
    }
}

Thank you so much, for the framework and the support!

jlynch1 commented 4 years ago

good find that for calling renewAccessToken() externally, that unless you override and set client.credential.oauthToken to "" you get the following error calling renewAccessToken()

requestError[Error Domain=OAuthSwiftError Code=401 "unauthorized_client Missing client_id()/client_secret() values"

there are use cases to calling this function externally and it would be better to enable this in the library or at least document the workaround in the wiki

Thanks