[GH-ISSUE #94] credential.auth2 variable is not automatically set to 'true' when using OAuth2Swift #58

Closed
opened 2026-03-03 16:45:18 +03:00 by kerem · 3 comments
Owner

Originally created by @ghost on GitHub (Aug 18, 2015).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/94

the oauth2 variable in OAuthSwiftCredential does not get set to true when authorizing with OAuth2. This means that the client does not use oauth2 headers (request.headers = ["Authorization": "Bearer \(self.credential.oauth_token)"]) when making network requests.

While this can be worked around by explicitly setting the oauth2 variable to true, shouldn't this happen by default??

Originally created by @ghost on GitHub (Aug 18, 2015). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/94 the `oauth2` variable in `OAuthSwiftCredential` does not get set to `true` when authorizing with OAuth2. This means that the client does not use oauth2 headers (`request.headers = ["Authorization": "Bearer \(self.credential.oauth_token)"]`) when making network requests. While this can be worked around by explicitly setting the `oauth2` variable to `true`, shouldn't this happen by default??
kerem closed this issue 2026-03-03 16:45:18 +03:00
Author
Owner

@phimage commented on GitHub (Aug 18, 2015):

The variable is set to true when token is filled

self.client.credential.oauth_token = accessToken
self.client.credential.oauth2 = true
success(credential: self.client.credential, response: response, parameters: responseParameters)

Are you talking about a scenario where you fill manually an old token?
In this case, you must set to true the oauth2 variable ;(

If not how you achieve this?

<!-- gh-comment-id:132125094 --> @phimage commented on GitHub (Aug 18, 2015): The variable is set to `true` when token is filled ``` swift self.client.credential.oauth_token = accessToken self.client.credential.oauth2 = true success(credential: self.client.credential, response: response, parameters: responseParameters) ``` Are you talking about a scenario where you fill manually an old token? In this case, you must set to `true` the `oauth2` variable ;( If not how you achieve this?
Author
Owner

@ghost commented on GitHub (Aug 18, 2015):

self.client.credential.oauth2 only gets set to true via a call to postOAuthAccessTokenWithRequestTokenByCode if responseParameters["code"] exists. If you are authorising via OAuth2 and responseParameters["access_token"] exists but responseParameters["code"] doesn't, then self.client.credential.oauth2 does not get set and defaults to false

            if let accessToken = responseParameters["access_token"] {
                self.client.credential.oauth_token = accessToken
                success(credential: self.client.credential, response: nil, parameters: responseParameters)
            }
            if let code = responseParameters["code"] {
                self.postOAuthAccessTokenWithRequestTokenByCode(code.stringByRemovingPercentEncoding!,
                    callbackURL:callbackURL,
                    success: { credential, response, responseParameters in
                        success(credential: credential, response: response, parameters: responseParameters)
                }, failure: failure)
            }
<!-- gh-comment-id:132235677 --> @ghost commented on GitHub (Aug 18, 2015): `self.client.credential.oauth2` only gets set to `true` via a call to `postOAuthAccessTokenWithRequestTokenByCode` if `responseParameters["code"]` exists. If you are authorising via OAuth2 and `responseParameters["access_token"]` exists but `responseParameters["code"]` doesn't, then `self.client.credential.oauth2` does not get set and defaults to `false` ``` if let accessToken = responseParameters["access_token"] { self.client.credential.oauth_token = accessToken success(credential: self.client.credential, response: nil, parameters: responseParameters) } if let code = responseParameters["code"] { self.postOAuthAccessTokenWithRequestTokenByCode(code.stringByRemovingPercentEncoding!, callbackURL:callbackURL, success: { credential, response, responseParameters in success(credential: credential, response: response, parameters: responseParameters) }, failure: failure) } ```
Author
Owner

@phimage commented on GitHub (Aug 25, 2015):

good point
so setting the oauth2 variable to true for that case solve this issue :

 if let accessToken = responseParameters["access_token"] {
                self.client.credential.oauth_token = accessToken
                self.client.credential.oauth2 = true
                success(credential: self.client.credential, response: nil, parameters: responseParameters)
 }
 if let code = responseParameters["code"] {

You can make a PR or I will do it later

<!-- gh-comment-id:134472728 --> @phimage commented on GitHub (Aug 25, 2015): good point so setting the oauth2 variable to true for that case solve this issue : ``` swift if let accessToken = responseParameters["access_token"] { self.client.credential.oauth_token = accessToken self.client.credential.oauth2 = true success(credential: self.client.credential, response: nil, parameters: responseParameters) } if let code = responseParameters["code"] { ``` You can make a PR or I will do it later
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#58
No description provided.