如何使用RestKit 0.20处理OAuth2 refreshToken

时间:2013-12-09 16:35:57

标签: oauth-2.0 restkit-0.20

我知道方法

-[[RKObjectManager sharedManager].HTTPClient setAuthorizationHeaderWithToken:]

我想这个方法接受accessToken。如何处理OAuth2 refreshToken?

1 个答案:

答案 0 :(得分:4)

好的,我解决了我的问题。 首先,您必须从http://goo.gl/zdTdlJ

下载AFOAuth2Client

这是AFHTTPClient扩展,它处理OAuth2身份验证,这里是如何初始化它并将其作为HTTPClient设置为RestKit的片段。

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