如何使用Swift在IOS应用程序中整合FitBit Api

首先,我在https://www.fitbit.com创build了一个帐户然后我在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创build的用户帐户的数据(名称,步骤等),我该怎么做? 我search,但无法findfitbit集成的任何教程。 以及在我的代码中使用这些信息的地方? 在这里输入图像说明 所以请引导我下一步该怎么做才能获取数据。

FitBit适用于需要客户端ID和密钥的OAuth 2.0 API。 您需要使用这些客户端ID和密钥才能使用OAuth 2.0 API进行身份validation。 在Swift上有一篇关于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" ) 

任何机会,你可以使用基本身份validation而不是OAuth? 我有一个类似的问题,试图邮寄到MailGun的一些自动化的电子邮件,我正在实施一个应用程序。

我能够通过一个大的HTTP响应来正确地工作。 我将完整path放到Keys.plist中,以便我可以将代码上传到github,并将一些参数分解成variables,以便稍后以编程方式设置它们。

 // 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() } 

Mailgunpath在Keys.plist中是一个名为mailgunAPIPath的string,值为:

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

希望这可以帮助!