简体   繁体   English

如何使用Swift在Fit应用程序Api中集成FitBit Api

[英]How to integrate FitBit Api in IOS app using Swift

First of all I created an account at https://www.fitbit.com Then I careated an app at https://dev.fitbit.com then installed OAuthSwift using cocoa pods and implemented this method in my AppDelegate 首先,我创建了一个帐户https://www.fitbit.com然后我careated在应用https://dev.fitbit.com然后安装OAuthSwift使用可可豆荚,在我的AppDelegate实现了这个方法

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

now i want to get the data (Name, Steps taken etc) of user account I created at https://www.fitbit.com how can I do that ? 现在我想获取我在https://www.fitbit.com创建的用户帐户的数据(名称,步骤等)我该怎么办? I searched but was not able to find any tutorial on fitbit integration. 我搜索但无法找到任何关于fitbit集成的教程。 And where to use this information in my code? 在我的代码中何处使用此信息? 在此输入图像描述 So please guide me about next step what should I do to get the data. 所以请指导下一步我应该怎么做才能获得数据。

FitBit work with OAuth 2.0 API which require Client ID and Secret key. FitBit使用OAuth 2.0 API,需要客户端ID和密钥。 You need these client id and secret key to authenticate with OAuth 2.0 API. 您需要这些客户端ID和密钥才能使用OAuth 2.0 API进行身份验证。 There is a blog post related to FitBit integration in iOS with Swift. 有一篇关于FitBit在iOS中与Swift集成的博客文章。 Lets checkout and learn "How to implement fitbit in iOS" https://appengineer.in/2016/04/30/fitbit-aouth-in-ios-app/ 让我们结帐并学习“如何在iOS中实现fitbit” https://appengineer.in/2016/04/30/fitbit-aouth-in-ios-app/

ex: 例如:

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"
    )

Any chance you could do it with basic auth as opposed to OAuth? 您是否有机会使用基本身份验证而不是OAuth? I had a similar problem trying to POST to MailGun for some automated emails I was implementing in an app. 我有一个类似的问题,尝试向MailGun发送一些我在应用程序中实现的自动电子邮件。

I was able to get this working properly with a large HTTP response. 我能够通过大量HTTP响应使其正常工作。 I put the full path into Keys.plist so that I can upload my code to github and broke out some of the arguments into variables so I can have them programmatically set later down the road. 我把完整的路径放到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()
}

The Mailgun Path is in Keys.plist as a string called mailgunAPIPath with the value: Mailgun Path在Keys.plist中作为名为mailgunAPIPath的字符串,其值为:

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

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM