ios - 如何在 NXOAuth2 中使用保存的 token ?
<p><p>我正在尝试使用 <a href="https://github.com/nxtbgthng/OAuth2Client" rel="noreferrer noopener nofollow">NXOauth2</a> pod 在我的 iOS 应用上处理 OAuth2 来实现解决方案。</p>
<p>我能够通过已安装的应用程序流程获取 OAuth2 属性,包括 <code>accessToken</code> 和 <code>refreshToken</code> - 并且该帐户正在保存到我的 <code>NXOAuth2AccountStore</code> 正确(通过检查 <code>[ accounts]</code> 进行验证)。</p>
<p>现在我已经保存了 token ,我想在后续应用打开时使用它们,而不是再次向用户请求访问权限。我在这里做这个:</p>
<pre><code>- (NSArray *)allAccounts
{
NXOAuth2AccountStore *store = ;
NSArray *accounts = ;
return accounts;
}
</code></pre>
<p>以下调用(用于获取一些用户详细信息)在用户完成审批流程后立即起作用,但在应用关闭并重新打开时不起作用。</p>
<pre><code>- (void)requestOAuth2ProtectedDetails
{
[NXOAuth2Request performMethod:@"GET"
onResource:
usingParameters:nil
withAccount:
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 = ;
NSLog(@"%@", userInfo);
}
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];
}
</code></pre>
<p>我应该如何使用已保存的 accessToken/refreshToken 来:</p>
<ol>
<li>使用<code>requestOAuth2ProtectedDetails</code>查询数据?</li>
<li>如果需要,使用我保存的 <code>refreshToken</code> 获取更新的 <code>accessToken</code>?</li>
</ol>
<p>这是我得到的错误:</p>
<pre><code>2014-06-23 09:41:17.040 Connector Got access token
2014-06-23 09:41:19.296 Connector *** Assertion failure in -, /Users/ericgross/Documents/Connector/Pods/NXOAuth2Client/Sources/OAuth2Client/NXOAuth2Client.m:82
2014-06-23 09:41:19.300 Connector *** 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 + + 136
3 Foundation 0x015874de - + 116
4 Connector 0x00011456 - + 502
5 Connector 0x00009a59 - + 841
6 Connector 0x0001f30b - + 667
7 Connector 0x0001e8d4 + + 404
8 Connector 0x00003eca - + 266
9 Connector 0x00002cba - + 282
10Connector 0x0000214f - + 415
11UIKit 0x0078633d - + 696
12UIKit 0x007865d9 - + 35
13UIKit 0x006a6267 - + 66
14UIKit 0x006a65ef - + 312
15UIKit 0x006a686b - + 49
16UIKit 0x006b13c8 - + 65
17UIKit 0x00661bc0 - + 2097
18UIKit 0x00666667 - + 824
19UIKit 0x0067af92 - + 3517
20UIKit 0x0067b555 - + 85
21UIKit 0x00668250 _UIApplicationHandleEvent + 683
22GraphicsServices 0x02fa7f02 _PurpleEventCallback + 776
23GraphicsServices 0x02fa7a0d PurpleEventCallback + 46
24CoreFoundation 0x01ba3ca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
25CoreFoundation 0x01ba39db __CFRunLoopDoSource1 + 523
26CoreFoundation 0x01bce68c __CFRunLoopRun + 2156
27CoreFoundation 0x01bcd9d3 CFRunLoopRunSpecific + 467
28CoreFoundation 0x01bcd7eb CFRunLoopRunInMode + 123
29UIKit 0x00665d9c - + 840
30UIKit 0x00667f9b UIApplicationMain + 1225
31Connector 0x0000443d main + 141
32libdyld.dylib 0x02265701 start + 1
33??? 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
</code></pre>
<p>第一次通过,这有效:</p>
<pre><code>- (void)performRequestWithSendingProgressHandler:(NXOAuth2ConnectionSendingProgressHandler)progressHandler
responseHandler:(NXOAuth2ConnectionResponseHandler)responseHandler;
{
NSAssert(self.me == nil, @"This object an only perform one request at the same time.");
NSMutableURLRequest *request = ;
;
self.connection = [ 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;
}
</code></pre>
<p>我注意到以下几点:</p>
<pre><code>(lldb) po self.account.oauthClient
<NXOAuth2Client: 0x8f1e760>
</code></pre>
<p>但在失败的情况下,会发生这种情况:</p>
<pre><code>(lldb) po self.account.oauthClient
2014-06-23 10:13:25.457 Connector *** Assertion failure in -, /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.
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>该库运行良好,成功登录后它会自动保存您的帐户。除非您明确说明,否则这些帐户不会从帐户存储中删除。
例如,我使用它来删除注销时的所有帐户:</p>
<pre><code>NXOAuth2AccountStore* store=;
for (NXOAuth2Account *account in ) {
;
}
</code></pre>
<p>请注意,当您在关闭应用程序时输入时不会删除您的帐户。</p>
<p>至于第二件事,我也在寻找解决办法</p></p>
<p style="font-size: 20px;">关于ios - 如何在 NXOAuth2 中使用保存的 token ?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24370996/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24370996/
</a>
</p>
页:
[1]