如何在NXOAuth2中使用已保存的令牌?

时间:2014-06-23 16:37:56

标签: ios objective-c oauth-2.0 oauth2client

我正在尝试使用NXOauth2窗格实现解决方案,以便在我的iOS应用上处理OAuth2。

我可以浏览已安装的应用程序流程以获取OAuth2属性,包括accessTokenrefreshToken - 该帐户正在保存到我的NXOAuth2AccountStore中(通过检查{{进行验证) 1}})。

现在我已经保存了令牌,我希望在后续的应用程序打开时使用它们,而不是再次请求用户访问。我在这里这样做:

[[NXOAuth2AccountStore sharedStore] accounts]

以下调用(以获取一些用户详细信息)在用户完成批准流程后立即生效,但在关闭并重新打开应用程序时无效。

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

我应该如何使用已保存的accessToken / refreshToken:

  1. 使用- (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); } }]; }
  2. 查询数据
  3. 如果需要,使用我保存的requestOAuth2ProtectedDetails
  4. 获取更新的accessToken

    以下是我遇到的错误:

    refreshToken

    第一次,这有效:

    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
    

    我注意到以下内容:

    - (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;
    }
    

    但在失败的情况下,会发生这种情况:

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

1 个答案:

答案 0 :(得分:1)

图书馆工作得很好,它会在成功登录后自动保存您的帐户。除非您明确说明,否则不会从帐户商店中删除帐户。 例如,我使用它来删除注销时的所有帐户:

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

当您关闭应用时,请注意您没有删除帐户。

关于第二个问题,我也在寻找解决方案