Fitbit OAuth2.0令牌请​​求(Swift 3)

时间:2017-01-13 10:48:18

标签: ios swift xcode oauth-2.0 fitbit

我目前正在尝试使用 OAuth2.0 授权我的用户。我目前正在使用以下库:https://github.com/p2/OAuth2。 这是我的代码:

let oauth2 = OAuth2CodeGrant(settings: [
    "client_id": "******",  //ID from Fitbit
    "client_secret": "***************************", //Secret from Fitbit
    "authorize_uri": "https://www.fitbit.com/oauth2/authorize",
    "token_uri": "https://api.fitbit.com/oauth2/token",  
    "redirect_uris": ["http://google.com/"],   
    "scope": "profile settings activity nutrition heartrate location nutrition profile settings sleep social weight",
    "secret_in_body": true,
    "keychain": false,        
    ] as OAuth2JSON)

这是我的授权功能:

func startAuthorization() {
    oauth2.logger = OAuth2DebugLogger(.trace)

    oauth2.authorize() { authParameters, error in
        if let params = authParameters {
            print("Authorized! Access token is in `oauth2.accessToken`")
            print("Authorized! Additional parameters: \(params)")
        }
        else {
            print("Authorization was cancelled or went wrong: \(error)")   // error will not be nil  
        }
    }
}

这是我的日志跟踪:

[Debug] OAuth2: Starting authorization
[Debug] OAuth2: No access token, checking if a refresh token is available
[Debug] OAuth2: I don't have a refresh token, not trying to refresh
[Debug] OAuth2: Opening authorize URL in system browser: https://www.fitbit.com/oauth2/authorize?state=35701F98&response_type=code&scope=profile+settings+activity+nutrition+heartrate+location+nutrition+profile+settings+sleep+social+weight&redirect_uri=http%3A%2F%2Fgoogle.com%2F&client_id=******

它完美无误地运行,但我只是没有获得访问令牌。 我发现的每个教程都在Swift 2.希望你能帮助我!

1 个答案:

答案 0 :(得分:0)

不确定这是否会对您有所帮助,但在演示应用中有一些适用于Fitbit的示例。它最近更新大约2个月前,所以它应该在Swift 3中。但是如果它不是,我会事先道歉。

位于:https://github.com/OAuthSwift/OAuthSwift/blob/master/Demo/Common/ViewController.swift

他们拥有Fitbit和Fitbit 2的代码。

搜索// MARK:Fitbit

编辑:我的坏,错误的图书馆......

我的回答是这个图书馆:

https://github.com/OAuthSwift/OAuthSwift