Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh token not firing automatically #11

Closed
allistoncarlos opened this issue Dec 13, 2017 · 5 comments
Closed

Refresh token not firing automatically #11

allistoncarlos opened this issue Dec 13, 2017 · 5 comments
Labels

Comments

@allistoncarlos
Copy link

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
Copy link
Member

phimage commented Dec 14, 2017

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
Copy link
Author

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
Copy link
Member

phimage commented Dec 14, 2017

any log or error?

there is a bug for some service #12

@allistoncarlos
Copy link
Author

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!

@phimage phimage closed this as completed Jun 26, 2018
@jlynch1
Copy link

jlynch1 commented Jan 10, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants