如何使用 Swift 将 FitBit Api 集成到 IOS 应用程序中

问题描述 投票:0回答:2

首先,我在 https://www.fitbit.com 创建了一个帐户 然后我在 https://dev.fitbit.com 关注了一个应用程序 然后使用 cocoa pod 安装 OAuthSwift 并在我的 AppDelegate 中实现此方法

    func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
    if (url.host == "oauth-callback") {
        OAuthSwift.handleOpenURL(url)
    }
    return true
}

现在我想获取我在 https://www.fitbit.com 创建的用户帐户的数据(名称、采取的步数等) 我该怎么办?我进行了搜索,但找不到任何有关 Fitbit 集成的教程。以及在我的代码中哪里使用这些信息? [![在此处输入图像描述][1]][1] 所以请指导我下一步应该做什么来获取数据。

ios swift rest oauth-2.0 fitbit
2个回答
1
投票

FitBit 使用 OAuth 2.0 API,需要客户端 ID 和密钥。您需要这些客户端 ID 和密钥来使用 OAuth 2.0 API 进行身份验证。 有一篇博客文章涉及 iOS 中 FitBit 与 Swift 的集成。 让我们查看并学习“如何在 iOS 中实现 fitbit” https://appengineer.in/2016/04/30/fitbit-auth-in-ios-app/

例如:

let oauthswift = OAuth2Swift(
        consumerKey:    fitbit_clientID,
        consumerSecret: fitbit_consumer_secret,
        authorizeUrl:   "https://www.fitbit.com/oauth2/authorize",
        accessTokenUrl: "https://api.fitbit.com/oauth2/token",
        responseType:   "token"
    )

0
投票

您是否可以使用基本身份验证而不是 OAuth 来完成此操作?我在尝试将我在应用程序中实现的一些自动电子邮件发布到 MailGun 时遇到了类似的问题。

我能够通过大型 HTTP 响应使其正常工作。我将完整路径放入 Keys.plist 中,以便我可以将代码上传到 github,并将一些参数分解为变量,这样我就可以稍后以编程方式设置它们。

// Email the FBO with desired information
// Parse our Keys.plist so we can use our path
var keys: NSDictionary?

if let path = NSBundle.mainBundle().pathForResource("Keys", ofType: "plist") {
    keys = NSDictionary(contentsOfFile: path)
}

if let dict = keys {
    // variablize our https path with API key, recipient and message text
    let mailgunAPIPath = dict["mailgunAPIPath"] as? String
    let emailRecipient = "[email protected]"
    let emailMessage = "Testing%20email%20sender%20variables"

    // Create a session and fill it with our request
    let session = NSURLSession.sharedSession()
    let request = NSMutableURLRequest(URL: NSURL(string: mailgunAPIPath! + "from=FBOGo%20Reservation%20%3Cscheduler@<my domain>.com%3E&to=reservations@<my domain>.com&to=\(emailRecipient)&subject=A%20New%20Reservation%21&text=\(emailMessage)")!)

    // POST and report back with any errors and response codes
    request.HTTPMethod = "POST"
    let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
        if let error = error {
            print(error)
        }

        if let response = response {
            print("url = \(response.URL!)")
            print("response = \(response)")
            let httpResponse = response as! NSHTTPURLResponse
            print("response code = \(httpResponse.statusCode)")
        }
    })
    task.resume()
}

Mailgun 路径在 Keys.plist 中作为名为 mailgunAPIPath 的字符串,其值为:

https://API:key-<my key>@api.mailgun.net/v3/<my domain>.com/messages?

希望这有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.