How can I use saved tokens in NXOAuth2?

2

I am trying to implement a solution using the NXOauth2 pod to handle OAuth2 on my iOS app.

I am able to go through the installed application flow to get OAuth2 properties including accessToken and refreshToken - and the account is being saved into my NXOAuth2AccountStore properly (verified by inspecting [[NXOAuth2AccountStore sharedStore] accounts]).

Now that I have saved the tokens, I want to use them on subsequent app opens rather than go through requesting access again from the user. I do this here:

- (NSArray *)allAccounts
{
    NXOAuth2AccountStore *store = [NXOAuth2AccountStore sharedStore];
    NSArray *accounts = [store accountsWithAccountType:kIDMOAuth2AccountType];
    return accounts;
}

The following call (to get some user details) works right after the user goes through the approval flow but it does not work when the app is closed and reopened.

- (void)requestOAuth2ProtectedDetails
{
    [NXOAuth2Request performMethod:@"GET"
                        onResource:[NSURL URLWithString:@"https://www.googleapis.com/oauth2/v1/userinfo"]
                   usingParameters:nil
                       withAccount:[self allAccounts][0]
               sendProgressHandler:^(unsigned long long bytesSend, unsigned long long bytesTotal) {
                   // e.g., update a progress indicator
               }
                   responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
                       // Process the response
                       if (responseData) {
                           NSError *error;
                           NSDictionary *userInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
                           NSLog(@"%@", userInfo);
                       }
                       if (error) {
                           NSLog(@"%@", error.localizedDescription);
                       }
                   }];
}

How should I use a saved accessToken / refreshToken to:

  1. Query data using requestOAuth2ProtectedDetails?
  2. Get an updated accessToken if required, using the refreshToken I have saved?

Here is the error I am getting:

2014-06-23 09:41:17.040 Connector[8495:60b] Got access token
2014-06-23 09:41:19.296 Connector[8495:60b] *** Assertion failure in -[NXOAuth2Client initWithClientID:clientSecret:authorizeURL:tokenURL:accessToken:tokenType:persistent:delegate:], /Users/ericgross/Documents/Connector/Pods/NXOAuth2Client/Sources/OAuth2Client/NXOAuth2Client.m:82
2014-06-23 09:41:19.300 Connector[8495:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No token or no authorize URL'
*** First throw call stack:
(
    0   CoreFoundation                      0x01c281e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x019a78e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01c28048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x015874de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   Connector                           0x00011456 -[NXOAuth2Client initWithClientID:clientSecret:authorizeURL:tokenURL:accessToken:tokenType:persistent:delegate:] + 502
    5   Connector                           0x00009a59 -[NXOAuth2Account oauthClient] + 841
    6   Connector                           0x0001f30b -[NXOAuth2Request performRequestWithSendingProgressHandler:responseHandler:] + 667
    7   Connector                           0x0001e8d4 +[NXOAuth2Request performMethod:onResource:usingParameters:withAccount:sendProgressHandler:responseHandler:] + 404
    8   Connector                           0x00003eca -[ESGOAuth2Manager requestOAuth2ProtectedDetails] + 266
    9   Connector                           0x00002cba -[ESGOAuth2Manager setup] + 282
    10  Connector                           0x0000214f -[ESGViewController viewDidLoad] + 415
    11  UIKit                               0x0078633d -[UIViewController loadViewIfRequired] + 696
    12  UIKit                               0x007865d9 -[UIViewController view] + 35
    13  UIKit                               0x006a6267 -[UIWindow addRootViewControllerViewIfPossible] + 66
    14  UIKit                               0x006a65ef -[UIWindow _setHidden:forced:] + 312
    15  UIKit                               0x006a686b -[UIWindow _orderFrontWithoutMakingKey] + 49
    16  UIKit                               0x006b13c8 -[UIWindow makeKeyAndVisible] + 65
    17  UIKit                               0x00661bc0 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 2097
    18  UIKit                               0x00666667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    19  UIKit                               0x0067af92 -[UIApplication handleEvent:withNewEvent:] + 3517
    20  UIKit                               0x0067b555 -[UIApplication sendEvent:] + 85
    21  UIKit                               0x00668250 _UIApplicationHandleEvent + 683
    22  GraphicsServices                    0x02fa7f02 _PurpleEventCallback + 776
    23  GraphicsServices                    0x02fa7a0d PurpleEventCallback + 46
    24  CoreFoundation                      0x01ba3ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    25  CoreFoundation                      0x01ba39db __CFRunLoopDoSource1 + 523
    26  CoreFoundation                      0x01bce68c __CFRunLoopRun + 2156
    27  CoreFoundation                      0x01bcd9d3 CFRunLoopRunSpecific + 467
    28  CoreFoundation                      0x01bcd7eb CFRunLoopRunInMode + 123
    29  UIKit                               0x00665d9c -[UIApplication _run] + 840
    30  UIKit                               0x00667f9b UIApplicationMain + 1225
    31  Connector                           0x0000443d main + 141
    32  libdyld.dylib                       0x02265701 start + 1
    33  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

The first time through, this works:

- (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
                                 responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
    NSAssert(self.me == nil, @"This object an only perform one request at the same time.");

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.resource];
    [request setHTTPMethod:self.requestMethod];
    self.connection = [[NXOAuth2Connection alloc] initWithRequest:request
                                                requestParameters:self.parameters
                                                      oauthClient:self.account.oauthClient
                                           sendingProgressHandler:progressHandler
                                                  responseHandler:responseHandler];
    self.connection.delegate = self;

    // Keep request object alive during the request is performing.
    self.me = self;
}

And I notice the following:

(lldb) po self.account.oauthClient
<NXOAuth2Client: 0x8f1e760>

But in the failure case, this happens:

(lldb) po self.account.oauthClient
2014-06-23 10:13:25.457 Connector[8678:60b] *** Assertion failure in -[NXOAuth2Client initWithClientID:clientSecret:authorizeURL:tokenURL:accessToken:tokenType:persistent:delegate:], /Users/ericgross/Documents/Connector/Pods/NXOAuth2Client/Sources/OAuth2Client/NXOAuth2Client.m:82
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-3)..
The process has been returned to the state before expression evaluation.
ios
objective-c
oauth-2.0
oauth2client
asked on Stack Overflow Jun 23, 2014 by Eric • edited Jun 23, 2014 by Eric

1 Answer

1

The library works pretty well,it automatically saves your accounts after a successful login was made. The accounts are not deleted from the account store unless you specifically say so. For example i use this to delete all account on logout:

NXOAuth2AccountStore* store=[NXOAuth2AccountStore sharedStore];
for (NXOAuth2Account *account in [store accounts]) {
    [store removeAccount:account];
}

see that you don't remove your accounts when you enter on close the app.

As for the second matter, i am searching for a solution as well

answered on Stack Overflow Aug 28, 2014 by Adrian

User contributions licensed under CC BY-SA 3.0