2015-09-16 17 views
0

我做的OAuth2使用的OAuth2 - https://github.com/trongdth/OAuth2-for-iOS, 我成功登錄,並得到響應如何在Fitbit API集成中使用訪問令牌請求 - iOS?

{ 
     "kOAuth_AccessToken" = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NDIzODY2NzEsInNjb3BlcyI6Indsb2Mgd3BybyB3bnV0IHdzbGUgd3NldCB3d2VpIHdociB3YWN0IHdzb2MiLCJzdWIiOiIzRFJQQzYiLCJhdWQiOiIyMjlROVEiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0NDIzODMwNzF9.5vTYvUAuvMflnOw_7cc1nZoighhtUx4RU26-Q7SewzQ"; 
     "kOAuth_AuthorizeURL" = "https://www.fitbit.com/oauth2/authorize"; 
     "kOAuth_Callback" = "http://oauth-callback/fitbit"; 
     "kOAuth_ClientId" = string; 
     "kOAuth_ExpiredDate" = ""; 
     "kOAuth_RefreshToken" = 97ad2a073f40f21974c8d27cdb74523047f39e98cd2adcfd6f6cc3eb92522d53; 
     "kOAuth_Scope" = "activity heartrate location nutrition profile settings sleep social weight"; 
     "kOAuth_Secret" = string; 
     "kOAuth_TokenURL" = "https://api.fitbit.com/oauth2/token"; 
    } 

這裏 - 我怎麼能在回調URI得到code URI參數值?

根據Fitbit文檔的---

訪問令牌請求: - 當用戶授權的授權碼格蘭特流應用程序,應用程序必須對接入令牌交換的授權碼。該代碼僅適用於10分鐘。

認證頭: -

Authorization頭應設置爲基本後跟一個空格和應用程序的客戶端ID的64位編碼和祕密用冒號連接起來。

我這樣做是通過使用AFNetworking LIB的,請對代碼的樣子(頭) -

-(AFHTTPSessionManager *)getSessionManager { 
    if (!self.sessionManager){ 
     NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
     sessionConfiguration.HTTPAdditionalHeaders = nil; 
     NSURL *url = [NSURL URLWithString:FITBIT_BASE_URL]; 
     self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:url sessionConfiguration:sessionConfiguration]; 
    } 

// [self.sessionManager.requestSerializer setValue:Appdelegate.fitbitAuthorizationString forHTTPHeaderField:@"Authorization"]; 
    [self.sessionManager.requestSerializer setAuthorizationHeaderFieldWithUsername:Appdelegate.fitbitOAuthClientId password:Appdelegate.fitbitOAuthClientSecret]; 


    self.sessionManager.responseSerializer = [JSONResponseSerializerWithData serializer]; 

    self.sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 

    return self.sessionManager; 

} 

,並要求令牌訪問 - 代碼(參數的)是

NSDictionary *tempParamsDict = @{@"client_id":[JsonUtil stringFromKey:@"kOAuth_ClientId" inDictionary:dictResponse], @"grant_type":@"authorization_code", @"redirect_uri":[JsonUtil stringFromKey:@"kOAuth_Callback" inDictionary:dictResponse], @"code":@""}; 

我的要求是

[dP postJsonContentWithUrl:@"oauth2/token" parameters:tempParamsDict completion:^(BOOL success, id responseObject, NSError *error) { 
      // NSLog(@"%@", responseObject); 
      if (success) { 
       NSLog(@"Response: %@", responseObject); 
      } 
      else { 
       NSLog(@"%@", error.localizedDescription); 
       /* 
       You got 404 response ,The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with the server, but the server could not find what was requested. 

       Make sure that you have valid url, try to put your link in the browser and see if it is correct, if the url you requested have special headers 
       */ 
      } 
     }]; 

我越來越 - 請求失敗:未找到(404)error。 我錯過了什麼,如何進一步獲取令牌並訪問Fitbit API?

更新 這是發生由於接入代碼後,我試圖訪問代碼,現在我收到此錯誤

description of error->_userInfo: 
{ 
    NSErrorFailingURLKey = "https://api.fitbit.com/oauth2/token.json"; 
    NSErrorFailingURLStringKey = "https://api.fitbit.com/oauth2/token.json"; 
    NSLocalizedDescription = cancelled; 
} 

感謝很多提前

+0

http://stackoverflow.com/questions/37520816/how-to-integrate-fitbit-api-in-ios-app-using-swift/39697291#39697291 –

回答

0

代替NSURLSession使用NSURLConnection到從Fitbit API請求數據。

相關問題