Igor
Igor

Reputation: 1332

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?

Upvotes: 1

Views: 1227

Answers (1)

Igor
Igor

Reputation: 1332

Ok, I resolve my problem. First of all, you have to download AFOAuth2Client from http://goo.gl/zdTdlJ

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
}];

Upvotes: 4

Related Questions