1

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?

2 Answers 2

1

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:...]

5
  • They each inherit from AFHTTPClient, So I don't think that's possible. AFRESTClient is defined in the AFIncrementalStore framework.
    – tomburns
    Jul 12, 2013 at 12:03
  • Thanks for the response! It looks like authorizationHeaderForMethod:path:parameters: is a private method as declared in AFOAuth1Client, so I'd have to modify the header to expose it (unless I'm missing something there). I doubt any harm would come of that, but I'd be interested to hear your thoughts. Thanks again for the help.
    – tomburns
    Jul 12, 2013 at 18:38
  • That makes me wrong twice. But there really seems to be very little guidance on this. I did find one AFOAuth2Client example project here: github.com/opro/opro_iphone_demo which uses the returned token by calling setAuthorizationHeaderWithToken on the main AFHTTPClient subclass. Try that, and if that does not work, consider sending a tweet or email to Matt Thompson. It would be great to have the expert weigh in on this.
    – KHansenSF
    Jul 12, 2013 at 19:19
  • I reached out to him via twitter, and opened an issue on AFIncrementalStore's GitHub page asking for best practices for either an existing solution or a patch. I'll reference this SE question as well. Here's the GitHub link, if you care to weigh in: Issue #226
    – tomburns
    Jul 12, 2013 at 19:30
  • Thanks. I'll watch the thread w/o weighing. I've embarrassed myself enough! :-)
    – KHansenSF
    Jul 12, 2013 at 19:34
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.