Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Managed to get this working by changing:</p> <pre><code> AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"subdomain.example.com"]; [self setAuthorizationHeaderWithToken:credential.accessToken]; </code></pre> <p>to:</p> <pre><code> AFOAuthCredential *credential = [AFOAuthCredential retrieveCredentialWithIdentifier:@"subdomain.example.com"]; NSString *authValue = [NSString stringWithFormat:@"Bearer %@", credential.accessToken]; [self setDefaultHeader:@"Authorization" value:authValue]; </code></pre> <p>UPDATE</p> <p>What I had failed to notice was that <code>AFOAuth2Client</code> is itself a subclass of <code>AFHTTPClient</code> so can be used as the base class of the API class, e.g.:</p> <pre><code>@interface YFExampleAPIClient : AFOAuth2Client + (YFExampleAPIClient *)sharedClient; /** */ - (void)authenticateWithUsernameAndPassword:(NSString *)username password:(NSString *)password success:(void (^)(AFOAuthCredential *credential))success failure:(void (^)(NSError *error))failure; @end </code></pre> <p>And the implementation becomes:</p> <pre><code>@implementation YFExampleAPIClient + (YFExampleAPIClient *)sharedClient { static YFExampleAPIClient *_sharedClient = nil; static dispatch_once_t onceToken; dispatch_once(&amp;onceToken, ^{ NSURL *url = [NSURL URLWithString:kClientBaseURL]; _sharedClient = [YFExampleAPIClient clientWithBaseURL:url clientID:kClientID secret:kClientSecret]; }); return _sharedClient; } - (void)authenticateWithUsernameAndPassword:(NSString *)username password:(NSString *)password success:(void (^)(AFOAuthCredential *credential))success failure:(void (^)(NSError *error))failure { [self authenticateUsingOAuthWithPath:@"oauth/token" username:username password:password scope:nil success:^(AFOAuthCredential *credential) { NSLog(@"Successfully received OAuth credentials %@", credential.accessToken); [self setAuthorizationHeaderWithCredential:credential]; success(credential); } failure:^(NSError *error) { NSLog(@"Error: %@", error); failure(error); }]; } - (id)initWithBaseURL:(NSURL *)url clientID:(NSString *)clientID secret:(NSString *)secret { self = [super initWithBaseURL:url clientID:clientID secret:secret]; if (!self) { return nil; } [self setDefaultHeader:@"Accept" value:@"application/json"]; return self; } @end </code></pre> <p>Note that <code>initWithBaseURL</code> is overridden to set the HTTP accept header.</p> <p>Full source code is available on GitHub - <a href="https://github.com/yellowfeather/rails-saas-ios" rel="noreferrer">https://github.com/yellowfeather/rails-saas-ios</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload