如何在 Fitbit API 集成中执行访问令牌请求 - iOS?

How to do Access Token Request in Fitbit API Integration - iOS?

我使用 OAuth2 实现了 OAuth2 - https://github.com/trongdth/OAuth2-for-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分钟内有效。

授权Header:-

Authorization header 应设置为 Basic,然后是 space 和应用程序客户端 ID 的 Base64 编码字符串以及与冒号.

我是用AFNetworking lib做的,请看代码(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
                 */
            }
        }];

我收到 - 请求失败:未找到 (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 请求数据。