Reputation: 11
How do I get AFOAuth1Client to work with AFIncrementalStore?
I'm able to get a valid instance of AFOAuth1Token using AFOAuth1Client, and I'm able to save it to the keychain for reuse. My singleton instance of AFOAuth1Client is able to make calls using the token, but I'm uncertain how to pass it along for use by my instance of AFRESTClient which implements the AFIncrementalStoreHTTPClient protocol.
AFRESTClient's method "setAuthorizationHeaderWithToken:" wants a string, and none of the attributes stored in the token seem to work. What do I need to do here? Am I going about this entirely incorrectly? My understanding was that the AFOAuth1Client should only be used to acquire a token, but I'm beginning to think that I may be off course.
Edit: Perhaps a more concise version of the question: How do I get AFOAuth1Client to work with AFIncrementalStore?
Upvotes: 1
Views: 569
Reputation: 11
One solid flash of inspiration later, here's the solution I went with:
In my subclass of AFRESTClient
, I overrode - (NSMutableRequest *)requestWithMethod:path:parameters:
like so:
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSMutableURLRequest *request = [[MyAFOAuth1ClientSubclass sharedClient]
requestWithMethod:method
path:path
parameters:parameters];
return request;
}
That got the AFRESTClient
making calls using the OAuth tokens as expected, and I was off to the races. I'll update this if I encounter any unintended side effects, but it seems pretty solid so far.
Upvotes: 0
Reputation: 604
I was going to suggest modifying AFRESTClient to inherit from AFOAuth1Client, but I found this pull request where Matt states that AFOAuth1Client is meant to be separate from your client subclass: https://github.com/AFNetworking/AFOAuth1Client/pull/6
I have used AFNetworking extensively, but had written my own authentication instead of using AFOAuth1Client. In that I overrode requestWithMethod in my AFHTTPClient subclass. The authorization header is not static, it depends on the path and parameters of each request.
Together with Matt's guidance above, I believe that you should add an authClient property to your Client subclass, then add the token calculated by that client to the requests in your own - similar to the way that the AFOAuth1Client does itself.
I am not sure of this, but I would give it a shot.
// In your AFRestClient subclass
@property(nonatomic, strong) AFOAuth1Client *authClient;
...
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
{
NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
NSString *authorizationHeader = [self.authClient authorizationHeaderForMethod:method path:path parameters:parameters];
[request setValue:authorizationHeader forHTTPHeaderField:@"Authorization"];
return request;
}
Before making your first request you will have initialized authClient and obtained the [self.authClient authorizeUsingOAuthWithRequestTokenPath:...]
Upvotes: 1