[GH-ISSUE #569] How do I set the token expiration date for OAuth 2.0 with Implicit Grant Flow? #371

Closed
opened 2026-03-03 16:48:08 +03:00 by kerem · 4 comments
Owner

Originally created by @aholsteinson on GitHub (Jan 24, 2020).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/569

Description:

I am using the Fitbit API to authorize my app and it is working fine however in the OAuthSwift documentation I don't see a way to set the expiration date of the generated token even tho the Fitbit API has a method to do so and the Implicit Grant Flow lets you set the expiration date to up to a year.

Here is my config for oAuthSwift:

let config = OAuth2Swift( consumerKey: Constants.FitbitApi.consumerKey, consumerSecret: Constants.FitbitApi.consumerSecret, authorizeUrl: Constants.FitbitApi.authorizeUrl, accessTokenUrl: Constants.FitbitApi.accessTokenUrl, responseType: Constants.FitbitApi.responseType )

Per de documentations those are the parameters it takes but no expiration date parameter. And here is my authorization method:

` @IBAction func doOAuthFitbit(sender: AnyObject) {
let oauthswift = FitbitOAuth.sharedInstance.config
oauthswift.accessTokenBasicAuthentification = true
FitbitOAuth.sharedInstance.oauthswift = oauthswift

    let state = generateState(withLength: 20)
    oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)

    guard let callbackURL = URL(string: Constants.FitbitApi.callBackUrl) else { return }

    let _ = oauthswift.authorize(withCallbackURL: callbackURL, scope: "weight", state: state) { result in
        switch result {
            case .success(let (credential, response, parameters)):
          
              FitbitOAuth.sharedInstance.saveOAuthInStorage()

              self.authorizationLabel.text = "Authorized"

         case .failure(let error):
             print(error.localizedDescription)

             self.authorizationLabel.text = "Not Authorized"
        }
    }
}`

As noted this is working fine however the token always comes with an expiration date of 277895 and I would like to set it to 30 days or a year.

Thanks in advance.

OAuth Provider? (Twitter, Github, ..):

Fitbit

OAuth Version:

  • Version 1
  • Version 2

OS (Please fill the version) :

  • iOS :
  • OSX :
  • TVOS :
  • WatchOS :

Installation method:

  • Carthage
  • CocoaPods
  • Swift Package Manager
  • Manually

Library version:

  • head
  • v2.1.0
  • v2.0.0
  • v1.4.1
  • v1.2 (Swift 4.0)
  • other: (Please fill in the version you are using.)

Xcode version:

  • 11.x (Swift 5.1)

  • 10.x (Swift 5.0)

  • 10.x (Swift 4.1)

  • 9.3 (Swift 4.1)

  • 9.0 (Swift 4.0)

  • other: (Please fill in the version you are using.)

  • objective c

Originally created by @aholsteinson on GitHub (Jan 24, 2020). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/569 ### Description: I am using the Fitbit API to authorize my app and it is working fine however in the OAuthSwift documentation I don't see a way to set the expiration date of the generated token even tho the Fitbit API has a method to do so and the Implicit Grant Flow lets you set the expiration date to up to a year. Here is my config for oAuthSwift: ` let config = OAuth2Swift( consumerKey: Constants.FitbitApi.consumerKey, consumerSecret: Constants.FitbitApi.consumerSecret, authorizeUrl: Constants.FitbitApi.authorizeUrl, accessTokenUrl: Constants.FitbitApi.accessTokenUrl, responseType: Constants.FitbitApi.responseType )` Per de documentations those are the parameters it takes but no expiration date parameter. And here is my authorization method: ` @IBAction func doOAuthFitbit(sender: AnyObject) { let oauthswift = FitbitOAuth.sharedInstance.config oauthswift.accessTokenBasicAuthentification = true FitbitOAuth.sharedInstance.oauthswift = oauthswift let state = generateState(withLength: 20) oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift) guard let callbackURL = URL(string: Constants.FitbitApi.callBackUrl) else { return } let _ = oauthswift.authorize(withCallbackURL: callbackURL, scope: "weight", state: state) { result in switch result { case .success(let (credential, response, parameters)): FitbitOAuth.sharedInstance.saveOAuthInStorage() self.authorizationLabel.text = "Authorized" case .failure(let error): print(error.localizedDescription) self.authorizationLabel.text = "Not Authorized" } } }` As noted this is working fine however the token always comes with an expiration date of 277895 and I would like to set it to 30 days or a year. Thanks in advance. ### OAuth Provider? (Twitter, Github, ..): Fitbit ### OAuth Version: - [ ] Version 1 - [x] Version 2 ### OS (Please fill the version) : - [x] iOS : - [ ] OSX : - [ ] TVOS : - [ ] WatchOS : ### Installation method: - [ ] Carthage - [x] CocoaPods - [ ] Swift Package Manager - [ ] Manually ### Library version: - [ ] head - [ ] v2.1.0 - [x] v2.0.0 - [ ] v1.4.1 - [ ] v1.2 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) ### Xcode version: - [x] 11.x (Swift 5.1) - [ ] 10.x (Swift 5.0) - [ ] 10.x (Swift 4.1) - [ ] 9.3 (Swift 4.1) - [ ] 9.0 (Swift 4.0) - [ ] other: (Please fill in the version you are using.) - [ ] objective c
kerem closed this issue 2026-03-03 16:48:09 +03:00
Author
Owner

@phimage commented on GitHub (Jan 27, 2020):

Hi
Fit bit api link about that please.
Sometimes token duration is defined on website interface

<!-- gh-comment-id:578691829 --> @phimage commented on GitHub (Jan 27, 2020): Hi Fit bit api link about that please. Sometimes token duration is defined on website interface
Author
Owner

@aholsteinson commented on GitHub (Jan 27, 2020):

Hi
Fit bit api link about that please.
Sometimes token duration is defined on website interface

Hi, here is the link to the Fitbit API documentation on oAuth 2:

https://dev.fitbit.com/build/reference/web-api/oauth2/

You can see that they let you specify a "expires_in" parameter when using Implicit Grant Flow.

<!-- gh-comment-id:578737255 --> @aholsteinson commented on GitHub (Jan 27, 2020): > Hi > Fit bit api link about that please. > Sometimes token duration is defined on website interface Hi, here is the link to the Fitbit API documentation on oAuth 2: https://dev.fitbit.com/build/reference/web-api/oauth2/ You can see that they let you specify a "expires_in" parameter when using Implicit Grant Flow.
Author
Owner

@phimage commented on GitHub (Jan 27, 2020):

you can try by adding to the parameter parameters
github.com/OAuthSwift/OAuthSwift@6ba5b8dd9f/Sources/OAuth2Swift.swift (L83)
parameters: ["expires_in": 3600]

<!-- gh-comment-id:578815239 --> @phimage commented on GitHub (Jan 27, 2020): you can try by adding to the parameter `parameters` https://github.com/OAuthSwift/OAuthSwift/blob/6ba5b8dd9f16c78332fd4a60d9770bbf97c66609/Sources/OAuth2Swift.swift#L83 `parameters: ["expires_in": 3600]`
Author
Owner

@aholsteinson commented on GitHub (Jan 27, 2020):

@phimage thanks, that worked!

Much appreciated.

<!-- gh-comment-id:578913962 --> @aholsteinson commented on GitHub (Jan 27, 2020): @phimage thanks, that worked! Much appreciated.
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#371
No description provided.