oauth-2.0restkit-0.20

How to handle OAuth2 refreshToken with RestKit 0.20


I know for method

-[[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithToken:]

and I suppose that this method accepts accessToken. How to handle OAuth2 refreshToken?


Solution

  • Ok, I resolve my problem. First of all, you have to download AFOAuth2Client from https://github.com/AFNetworking/AFOAuth2Manager/

    This is AFHTTPClient extension witch handles OAuth2 authentication and here's snippet how to initialize it and set it into RestKit as HTTPClient.

    AFOAuthCredential* credential = [AFOAuthCredential credentialWithOAuthToken:accessToken tokenType:kAFOAuthCodeGrantType];
    [credential setRefreshToken:refreshToken expiration:expirationDate];
    AFOAuth2Client* oAuth2Client = [AFOAuth2Client clientWithBaseURL:[NSURL URLWithString:baseUrl]
                                                                    clientID:clientID
                                                                      secret:secret];
    [oAuth2Client setAuthorizationHeaderWithToken:accessToken];
    
    // This line is for environment where you don't have valid certificate.
    // For example for development environment
    [oAuth2Client setAllowsInvalidSSLCertificate:YES];
    
    [[RKObjectManager sharedManager] setHTTPClient:oAuth2Client];
    [[RKObjectManager sharedManager] getObjectsAtPath:path parameters:nil 
         success:^(
           RKObjectRequestOperation* operation, RKMappingResult* mappingResult) {
               // Handle success response
         } failure:^(RKObjectRequestOperation* operation, NSError* error) {
              // Handle failure response
    }];