I’m trying get RestKit (version 0.10.2) to authenticate with OAuth2. I’m using GTMOAuth2 to handle the OAuth interactions.
I’ve successfully gotten GTMOAuth2 to sign me in and make a request to the api I’m using.
I’ve also managed to get RestKit to make a request with my access token with this:
- (void)setRKAuthorizationToken:(NSString *)authorizationToken {
RKObjectManager* objectManager = [RKObjectManager sharedManager];
NSString* authHeader = [NSString stringWithFormat:@"Bearer %@", authorizationToken];
[objectManager.client setValue:authHeader forHTTPHeaderField:@"Authorization"];
}
In this code sample I am manually setting the HTTP header because RestKit’s support for OAuth2 sets the header as Authorization: OAuth2 <accessToken> instead of Authorization: Bearer <accessToken>.
Anyway, this works great until the access token needs to be refreshed with the refresh token.
What I’d really like to do is tell RestKit to use GTMOAuth2Authentication‘s - (BOOL)authorizeRequest:(NSMutableURLRequest *)request; as it automatically fetches a new access token with the refresh token when the access token expires.
BTW, RestKit is phasing it’s support for OAuth; authorizing requests with a third-party library is the suggested approach. I asked for an example and the response pointed me in the direction of classes to subclass, which are in the development branch.
So, the question is: Have you successfully integrated RestKit 0.10.x with GTMOAuth2 or know how to accomplish this?
I’m going to answer my own question here because I have something that works, but it’s not ideal.
Basically I am continuing to sign requests manually, and to deal with the possibility of an expired token, I am using GTMOAuth to authorize a dummy request. The serious downside is that it means an extra request to the api. But, now I know I have a valid access token. Part of the trick is going to be knowing when to call
loginWithBaseController:.Here is the basic concept:
Anyway, I am still looking for a better solution. I thought I would post this in case it helps someone else at least through development.