我正在尝试使用 NXOauth2 pod 在我的 iOS 应用上处理 OAuth2 来实现解决方案。
我能够通过已安装的应用程序流程获取 OAuth2 属性,包括 accessToken
和 refreshToken
- 并且该帐户正在保存到我的 NXOAuth2AccountStore
正确(通过检查 [[NXOAuth2AccountStore sharedStore] accounts]
进行验证)。
现在我已经保存了 token ,我想在后续应用打开时使用它们,而不是再次向用户请求访问权限。我在这里做这个:
- (NSArray *)allAccounts
{
NXOAuth2AccountStore *store = [NXOAuth2AccountStore sharedStore];
NSArray *accounts = [store accountsWithAccountType:kIDMOAuth2AccountType];
return accounts;
}
以下调用(用于获取一些用户详细信息)在用户完成审批流程后立即起作用,但在应用关闭并重新打开时不起作用。
- (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);
}
}];
}
我应该如何使用已保存的 accessToken/refreshToken 来:
requestOAuth2ProtectedDetails
查询数据?refreshToken
获取更新的 accessToken
?这是我得到的错误:
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 handleFailureInMethodbject: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 performMethodnResource: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)performRequestWithSendingProgressHandlerNXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandlerNXOAuth2ConnectionResponseHandler)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>
但在失败的情况下,会发生这种情况:
(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.
该库运行良好,成功登录后它会自动保存您的帐户。除非您明确说明,否则这些帐户不会从帐户存储中删除。 例如,我使用它来删除注销时的所有帐户:
NXOAuth2AccountStore* store=[NXOAuth2AccountStore sharedStore];
for (NXOAuth2Account *account in [store accounts]) {
[store removeAccount:account];
}
请注意,当您在关闭应用程序时输入时不会删除您的帐户。
至于第二件事,我也在寻找解决办法
关于ios - 如何在 NXOAuth2 中使用保存的 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24370996/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |