[GH-ISSUE #520] accesstoken in headers is not getting updated when token is renewed #340

Closed
opened 2026-03-03 16:47:51 +03:00 by kerem · 5 comments
Owner

Originally created by @sandeepk000 on GitHub (Mar 6, 2019).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/520

open func startAuthorizedRequest(_ url: String, method: OAuthSwiftHTTPRequest.Method, parameters: OAuthSwift.Parameters, headers: OAuthSwift.Headers? = nil...) {
return self.client.request(url, method: method, parameters: parameters, headers: headers, body: body, success: success) { (error) in
switch error {

        case OAuthSwiftError.tokenExpired:
            _ = self.renewAccessToken(withRefreshToken: self.client.credential.oauthRefreshToken, headers: renewHeaders ?? headers, success: { (credential, _, _) in
                // Ommit response parameters so they don't override the original ones
                // We have successfully renewed the access token.

                // If provided, fire the onRenewal closure
                if let renewalCallBack = onTokenRenewal {
                    renewalCallBack(credential)
                }
             
                // Reauthorize the request again, this time with a brand new access token ready to be used.
                _ = self.startAuthorizedRequest(url, method: method, parameters: parameters, headers: headers, body: body, onTokenRenewal: onTokenRenewal, success: success, failure: failure)
            },
                                      failure: failure)

}

here after token renewing previous token is used in headers(i mean same headers of first request are getting passed in 2nd request) in -=self.startAuthorized() request

first request after token renewal is failing every time and getting
OAuthSwiftResponse:{
error = "invalid_request";
"error_description" = "Missing form parameter: grant_type";
}

This problem i am getting only when token expiration is handled by library when i am checking token expiration and sending request no error like this is coming.Please help.

Originally created by @sandeepk000 on GitHub (Mar 6, 2019). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/520 open func startAuthorizedRequest(_ url: String, method: OAuthSwiftHTTPRequest.Method, parameters: OAuthSwift.Parameters, headers: OAuthSwift.Headers? = nil...) { return self.client.request(url, method: method, parameters: parameters, headers: headers, body: body, success: success) { (error) in switch error { case OAuthSwiftError.tokenExpired: _ = self.renewAccessToken(withRefreshToken: self.client.credential.oauthRefreshToken, headers: renewHeaders ?? headers, success: { (credential, _, _) in // Ommit response parameters so they don't override the original ones // We have successfully renewed the access token. // If provided, fire the onRenewal closure if let renewalCallBack = onTokenRenewal { renewalCallBack(credential) } // Reauthorize the request again, this time with a brand new access token ready to be used. _ = self.startAuthorizedRequest(url, method: method, parameters: parameters, headers: headers, body: body, onTokenRenewal: onTokenRenewal, success: success, failure: failure) }, failure: failure) } here after token renewing previous token is used in headers(i mean same headers of first request are getting passed in 2nd request) in -=self.startAuthorized() request first request after token renewal is failing every time and getting OAuthSwiftResponse:{ error = "invalid_request"; "error_description" = "Missing form parameter: grant_type"; } This problem i am getting only when token expiration is handled by library when i am checking token expiration and sending request no error like this is coming.Please help.
kerem closed this issue 2026-03-03 16:47:52 +03:00
Author
Owner

@sandeepk000 commented on GitHub (Mar 6, 2019):

i am sure that this is because in OAuthSwiftHTTPRequest.swift
public mutating func updateRequest(credential: OAuthSwiftCredential)
line 406 self.urlRequest.allHTTPHeaderFields = requestHeaders + headers
requestHeaders contains Authorization:Bearer already and in headers also Authorization Bearer is present in library both are getting added .Please Help

when requestHeaders is containing Authorization Bearer then i am getting this error

<!-- gh-comment-id:470241948 --> @sandeepk000 commented on GitHub (Mar 6, 2019): i am sure that this is because in OAuthSwiftHTTPRequest.swift public mutating func updateRequest(credential: OAuthSwiftCredential) line 406 self.urlRequest.allHTTPHeaderFields = requestHeaders + headers requestHeaders contains Authorization:Bearer already and in headers also Authorization Bearer is present in library both are getting added .Please Help when requestHeaders is containing Authorization Bearer then i am getting this error
Author
Owner

@sandeepk000 commented on GitHub (Mar 7, 2019):

Please help I am stuck only in this except this everything is working

<!-- gh-comment-id:470374720 --> @sandeepk000 commented on GitHub (Mar 7, 2019): Please help I am stuck only in this except this everything is working
Author
Owner

@sandeepk000 commented on GitHub (Mar 7, 2019):

hello sir,

i got this solved by adding this
if requestHeaders.count > 0{
headers.removeValue(forKey: "Authorization")
}
self.urlRequest.allHTTPHeaderFields = requestHeaders + headers

in public mutating func updateRequest(credential: OAuthSwiftCredential) {
}

<!-- gh-comment-id:470401201 --> @sandeepk000 commented on GitHub (Mar 7, 2019): hello sir, i got this solved by adding this if requestHeaders.count > 0{ headers.removeValue(forKey: "Authorization") } self.urlRequest.allHTTPHeaderFields = requestHeaders + headers in public mutating func updateRequest(credential: OAuthSwiftCredential) { }
Author
Owner

@phimage commented on GitHub (Mar 7, 2019):

Please do not add new message asking just for help. Do u know that people sleep and work :p

So you want authorization from requestHeaders, not headers with priority now
The question is why there is redundant header. I need to read the code later

<!-- gh-comment-id:470404397 --> @phimage commented on GitHub (Mar 7, 2019): Please do not add new message asking just for help. Do u know that people sleep and work :p So you want authorization from requestHeaders, not headers with priority now The question is why there is redundant header. I need to read the code later
Author
Owner

@sandeepk000 commented on GitHub (Mar 7, 2019):

hi i want to know whether this function will be called for making all headers or only when token is refreshed

/// Allow to customize computed headers
public protocol OAuthSwiftCredentialHeadersFactory {
func make(_ url: URL, method: OAuthSwiftHTTPRequest.Method, parameters: OAuthSwift.Parameters, body: Data?) -> [String: String]
}

<!-- gh-comment-id:470413740 --> @sandeepk000 commented on GitHub (Mar 7, 2019): hi i want to know whether this function will be called for making all headers or only when token is refreshed /// Allow to customize computed headers public protocol OAuthSwiftCredentialHeadersFactory { func make(_ url: URL, method: OAuthSwiftHTTPRequest.Method, parameters: OAuthSwift.Parameters, body: Data?) -> [String: String] }
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#340
No description provided.