如何在Swift中集成FitBit API到iOS应用程序

5

首先我在https://www.fitbit.com创建了一个账号, 然后在https://dev.fitbit.com创建了一个应用程序, 接下来使用cocoa pods安装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集成的任何教程。我应该在我的代码中哪里使用这些信息?请指导我下一步该怎么做才能获取数据。enter image description here

https://omarmetwally.quora.com/在iOS应用中集成Fitbit-API 希望有所帮助 - 7vikram7
@7vikram7 我也见过那个,但我还是不懂。 - Umair Afzal
2个回答

1

FitBit使用OAuth 2.0 API,需要客户端ID和密钥。您需要这些客户端ID和密钥来进行OAuth 2.0 API身份验证。 有一篇与Swift的iOS中FitBit集成相关的博客文章。 让我们看看并学习“如何在iOS中实现fitbit” https://appengineer.in/2016/04/30/fitbit-aouth-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发送POST请求时遇到了类似的问题。

我能够通过大型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 = "bar@foo.com"
    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?

希望这能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接