在Fitbit API集成-iOS中如何访问令牌请求? [英] How to do Access Token Request in Fitbit API Integration - iOS?

查看:199
本文介绍了在Fitbit API集成-iOS中如何访问令牌请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用OAuth2进行了OAuth2- https://github.com/trongdth/OAuth2-for -iOS
我已成功登录并得到响应

I did OAuth2 by using OAuth2 - https://github.com/trongdth/OAuth2-for-iOS , I successfully logged in and got response

{
        "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中的URI参数值?

Here - how can I get code URI parameter value in the callback URI?

根据 Fitbit 文档的---

访问令牌请求:-
当用户授权您在授权码授予流程中,如果您的应用程序未使用,则您的应用程序必须将授权码交换为访问令牌。该代码仅有效10分钟。

Access Token Request:- When a user authorizes your application in the Authorization Code Grant flow, your application must exchange the authorization code for an access token. The code is only valid for 10 minutes.

授权标头 :-

Authorization Header:-

Authorization标头应该设置为Basic,后跟空格和应用程序的客户端ID以及由冒号连接的密码的Base64编码字符串。

The Authorization header should be set to Basic followed by a space and a Base64 encoded string of your application's client id and secret concatenated with a colon.

我是通过使用 AFNetworking lib来做到这一点的,请查看代码(标题)-

I did this by making use of AFNetworking lib, Please have a look on code (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;

}

并请求令牌访问-代码(参数)为

and requested token access - code (parameter's) is

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)错误
我错过了什么,如何进一步获取令牌和访问Fitbit API?

I am getting - Request failed: not found (404) error. What have I missed and How can I proceed further to get token and access Fitbit APIs?

更新
由于访问代码而发生,所以我尝试访问代码,现在我收到此错误

Update That was happening due to access code so I tried to access code, now I am getting this error

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

非常感谢

推荐答案

使用 NSURLConnection 代替 NSURLSession 来向Fitbit请求数据API。

Instead of NSURLSession use NSURLConnection to request data from Fitbit APIs.

这篇关于在Fitbit API集成-iOS中如何访问令牌请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆