2016-02-09 6 views
3

Я работаю над интеграцией Fitbit. Как я могу сделать запрос и разрешить его API OAuth 2.0. Я проверил ASIHTTPRequest, но он недоступен в ARC. Могу ли я сделать это с помощью AFNetworking? Я использую Objective-C.Как назвать API OAuth 2.0 от iOS

+0

https://dev.fitbit.com/docs/oauth2/ https: //omarmetwally.quora. com/Integrating-the-Fitbit-API-in-iOS-приложения – Vizllx

+0

ссылаются на это: http://stackoverflow.com/questions/19102373/afnetworking-2-0-and-http-basic-authentication –

ответ

3

его сделали с помощью AFNetworking используя AFOAuth2Manager здесь код

В ViewController.m

#define CLIENT_ID   @"your client id" 
#define CONSUMER_SECRET  @"your consumer secret" 

авторизации Старт с помощью сафари.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=227FKJ&redirect_uri=http%3A%2F%2Fcallback.com%2Ffitbit&scope=activity%20nutrition%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight"]]; 

Получить код ответа от NSUserDefaults

NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:@"auth_code"]; 
    NSURL *baseURL = [NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize"]; 
    AFOAuth2Manager *OAuth2Manager = 
    [[AFOAuth2Manager alloc] initWithBaseURL:baseURL 
            clientID:CLIENT_ID secret:CONSUMER_SECRET]; 
    NSDictionary *dict = @{@"client_id":CLIENT_ID, @"grant_type":@"authorization_code",@"redirect_uri":@"http://lampdemos.com/fitbit",@"code":string}; 

    [OAuth2Manager authenticateUsingOAuthWithURLString:@"https://api.fitbit.com/oauth2/token" parameters:dict success:^(AFOAuthCredential *credential) { 
     NSLog(@"Token: %@", credential.accessToken); 
     AppDelegate *adddel = (AppDelegate *)[[UIApplication sharedApplication]delegate]; 
     adddel.credential = credential; 
     [self getUserProfileWithCredentials:credential]; 
    } failure:^(NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

вызова дополнительных запросов

-(void)getUserProfileWithCredentials:(AFOAuthCredential*)credential{ 
    NSURL *baseURL = [NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize"]; 

    AFHTTPRequestOperationManager *manager = 
    [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL]; 
    [manager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential]; 
    manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
    [manager GET:@"https://api.fitbit.com/1/user/-/profile.json" 
     parameters:nil 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 

      NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; 
      NSDictionary *userDict =[dictResponse valueForKey:@"user"]; 
      NSLog(@"Success: %@", userDict); 
      NSMutableString *str =[NSMutableString stringWithFormat:@"%@",@""]; 
      for (NSString *key in [userDict allKeys]) { 
       [str appendFormat:@"%@ %@\n",key,[NSString stringWithFormat:@"%@",[userDict valueForKey:key]]]; 
      } 
      self.profileDetails.text =str; 

     } 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"Failure: %@", error); 
     }]; 
}