如何使用 Swift 在 IOS 应用程序中集成 FitBit Api
How to integrate FitBit Api in IOS app using Swift
首先,我在 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 集成的教程。在我的代码中的什么地方使用这些信息?
因此,请指导我下一步应该如何获取数据。
FitBit 使用 OAuth 2.0 API,这需要客户端 ID 和密钥。您需要这些客户端 ID 和密钥才能使用 OAuth 2.0 API 进行身份验证。
在 iOS 和 Swift 中有一个与 FitBit 集成相关的博客 post。
让我们结帐并学习 "How to implement fitbit in iOS"
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"
)
您是否有机会使用基本身份验证而不是 OAuth 来做到这一点?对于我在应用程序中实现的一些自动电子邮件,我在尝试 POST 到 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 = "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?
希望对您有所帮助!
首先,我在 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 集成的教程。在我的代码中的什么地方使用这些信息?
FitBit 使用 OAuth 2.0 API,这需要客户端 ID 和密钥。您需要这些客户端 ID 和密钥才能使用 OAuth 2.0 API 进行身份验证。 在 iOS 和 Swift 中有一个与 FitBit 集成相关的博客 post。 让我们结帐并学习 "How to implement fitbit in iOS" 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"
)
您是否有机会使用基本身份验证而不是 OAuth 来做到这一点?对于我在应用程序中实现的一些自动电子邮件,我在尝试 POST 到 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 = "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?
希望对您有所帮助!