如何在Fitbit API集成中使用访问令牌请求 – iOS?

我通过使用OAuth2 – https://github.com/trongdth/OAuth2-for-iOS做了OAuth2,我成功login并得到了回复

{ "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"; } 

这里 – 我怎样才能获得callbackURI中的code URI参数值?

根据Fitbit doc的—

访问令牌请求: –当用户在授权代码授权stream程中授权您的应用程序时,您的应用程序必须交换访问令牌的授权代码。 该代码只有10分钟有效。

授权标题 : –

授权标头应设置为基本,后跟一个空格和一个Base64编码的应用程序的客户端ID和密码string与冒号连接。

我通过使用AFNetworking库来做到这一点,请看代码(header) –

 -(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 */ } }]; 

我越来越 – 请求失败:未find(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; } 

非常感谢

而不是NSURLSession使用NSURLConnection从Fitbit API请求数据。