get authorization from Fitbit using Oauth in iOS

試著忘記壹切 提交于 2019-12-02 04:17:18

Note - According to https://dev.fitbit.com/docs/oauth2/

  • Applications should upgrade to OAuth 2.0 by March 14, 2016
  • Use safari or SFSafariViewController to open authorization page

Solution starts from here

please replace CLIENT_ID, REDIRECT_URI and other text to correct information

Point1-

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&scope=activity%20nutrition%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight"]];

give proper scheme url, so that after successful login you will be redirected to your application. In openURL method you will get a OAUTHCODE

Point2-

Now get OAUTHTOKEN by using this OAUTHCODE

-(void)toGetRequestToken:(id)sender
{
    NSString *strCode  = [[NSUserDefaults standardUserDefaults] valueForKey:@"auth_code"];
    NSURL *baseURL = [NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize"];

AFOAuth2Manager *OAuth2Manager = [AFOAuth2Manager managerWithBaseURL:baseURL clientID:CLIENT_ID secret:CONSUMER_SECRET];
    OAuth2Manager.responseSerializer.acceptableContentTypes = [OAuth2Manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    NSDictionary *dict = @{@"client_id":CLIENT_ID, @"grant_type":@"authorization_code",@"redirect_uri":@"Pro-Fit://fitbit",@"code":strCode};
    [OAuth2Manager authenticateUsingOAuthWithURLString:@"https://api.fitbit.com/oauth2/token" parameters:dict success:^(AFOAuthCredential *credential) {

    // you can save this credential object for further use 
    // inside it you can find access token also 
    NSLog(@"Token: %@", credential.accessToken);

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

Point3-

now you can hit other FitBit requests like for "UserProfile" --

-(void)getFitbitUserProfile:(AFOAuthCredential*)credential{

    NSURL *baseURL = [NSURL URLWithString:@"https://www.fitbit.com/oauth2/authorize"];

    AFHTTPSessionManager *manager =
    [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    [manager.requestSerializer setAuthorizationHeaderFieldWithCredential:credential];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"https://api.fitbit.com/1/user/-/profile.json"
  parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

          NSDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
          NSDictionary *userDict  =[dictResponse valueForKey:@"user"];
          NSLog(@"Success: %@", userDict);          
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
          NSLog(@"Failure: %@", error);
    }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!