AFNetworking 2.0 with OAuth2 Tokens

I am trying to find a way to use OAuth2(access and update tokens) with AFNetworking. It’s hard for me to find good documentation on this.

I know about the project AFOAuth2Client, but only works for AFNetworking 1.x. Are there any good solutions for AFNetworking 2.0?

In addition, how exactly will tokens be updated? Should I always check the expiration of the token for each request? Where exactly can this be done? Or maybe use a timer when the token expires, and then select a new one?

+4
source share
4 answers

AFOAuth2Client AFNetworking 2.0. :

https://github.com/AFNetworking/AFOAuth2Client/pull/55#commits-pushed-5ed7a6e

AFNetworking 2.0. , CocoaPods.

0

, , , , , .

, , , , .

- (void)refreshAccessTokenWithSuccess:(APIClientSuccess)success
                              failure:(APIClientFailure)failure {
    if (self.credential == nil) {
        if (failure) {
            // Failed to get credentials
        }
        return;
    }
    if (!self.credential.isExpired) {
        if (success) {
            success(nil, nil);
        }
        return;
    }
    [self authenticateUsingOAuthWithURLString:kOAuthURL
                            refreshToken:self.credential.refreshToken
                                 success:^(AFOAuthCredential *newCredential) {
                                     // SUCCESS!
                                     self.credential = newCredential;
                                     if (success) {
                                         success(nil, nil);
                                     }
                                 }
                                 failure:^(NSError *error) {
                                     // Failed to get credentials
                                     if (failure) {
                                         failure(nil, error);
                                     }
                                 }];
}
- (void)RefreshGET:(NSString *)URLString
 parameters:(NSDictionary *)parameters
    success:(APIClientSuccess)success
    failure:(APIClientFailure)failure
{
    APIClientSuccess nestedSuccess  = ^(AFHTTPRequestOperation *operation, id responseObject) {
        AFHTTPRequestOperation *thisRequestOperation = [super GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (success) {
                success((AFHTTPRequestOperation *)operation, responseObject);
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            // Failed request
            if (failure) {
                failure((AFHTTPRequestOperation *)operation, error);
            }
        }];
    };
    [self refreshAccessTokenWithSuccess:nestedSuccess failure:failure];
}

, APIClient.

+1

, , , . API , GMail OAuth API, API. API / .

. , , , API. , , - X-Authorization API, .

, , , -, .

0

AFOAuth2Client (Gabriel Rinaldi) AFNetworking 2.0 ( responseObject SSL). pull ( , ). GitHub

Cocoapods

pod 'GROAuth2Client', :git => 'https://github.com/nicktmro/GROAuth2SessionManager'

Gabriel pod pod "GROAuth2SessionManager" , "~> 0.2".

I understand that this does not answer the second part of your question regarding updating the token. I'm not so far in my project, so I can’t tell you for sure, but I think you can find out pretty quickly if it updates it or not.

0
source

Source: https://habr.com/ru/post/1529485/


All Articles