簡體   English   中英

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

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

首先,我創建了一個帳戶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
}

現在我想獲取我在https://www.fitbit.com創建的用戶帳戶的數據(名稱,步驟等)我該怎么辦? 我搜索但無法找到任何關於fitbit集成的教程。 在我的代碼中何處使用此信息? 在此輸入圖像描述 所以請指導下一步我應該怎么做才能獲得數據。

FitBit使用OAuth 2.0 API,需要客戶端ID和密鑰。 您需要這些客戶端ID和密鑰才能使用OAuth 2.0 API進行身份驗證。 有一篇關於FitBit在iOS中與Swift集成的博客文章。 讓我們結帳並學習“如何在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"
    )

您是否有機會使用基本身份驗證而不是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 = "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 Path在Keys.plist中作為名為mailgunAPIPath的字符串,其值為:

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

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM