【发布时间】:2017-07-13 05:00:45
【问题描述】:
我正在尝试在基于 Objective C 的应用程序中使用 OAuthSwift 库提取 FitBit 用户数据。一旦用户点击“允许”并显示回调 url,我无法让 webview 关闭,但不会触发授权函数的成功或失败块,因此我也无权访问任何数据。它只是停在回调 URL 上,用户无法退出 web 视图。
所有这些都是对 documentation 从 swift 到 Objective c 的重新解释,所以我的翻译可能存在问题,但我不明白为什么它不起作用,并且基于我在他们(和其他people's) github 该库与基于 Obj-C 的应用程序兼容。
在我的 AppDelegate 中有:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(nullable NSString *)sourceApplication annotation:
(id)annotation
{
if([url.host isEqual: @"CALLBACKURL"])
{
[OAuthSwift handleWithUrl:url];
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Dismiss View");
}
NSLog(@"THIS IS BEING CALLED");
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if([url.host isEqual: @"CALLBACKURL"])
{
[OAuthSwift handleWithUrl:url];
[self.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"Dismiss View");
}
NSLog(@"THIS IS BEING CALLED");
return YES;
}
在呈现身份验证过程的视图控制器中:
- (IBAction)synchFitBit:(id)sender
{
NSDictionary *parameters = @{
@"consumerKey": @"MYKEY",
@"consumerSecret": @"MYSECRET",
@"authorizeUrl": @"https://www.fitbit.com/oauth2/authorize",
@"accessTokenUrl": @"https://api.fitbit.com/oauth2/token",
@"responseType" : @"code"
};
OAuth2Swift *oauth = [[OAuth2Swift alloc] initWithParameters:parameters];
oauth.client.paramsLocation = ParamsLocationRequestURIQuery;
SafariURLHandler *safariURLHandler = [[SafariURLHandler alloc] initWithViewController:self oauthSwift:oauth];
safariURLHandler.delegate = self;
oauth.authorizeURLHandler = safariURLHandler;
[oauth objc_authorizeWithCallbackURL:@"CALLBACKURL" scope:@"activity" state:@"" parameters:parameters headers:parameters
success:^(OAuthSwiftCredential *credential, OAuthSwiftResponse *response, NSDictionary<NSString *,id> *parameters) {
NSString *userId = parameters[@"userid"];
NSLog(@"SUCSESS: %@",userId);
}
failure:^(NSError *error) {
NSLog(@"FAILURE");
}];
}
这些函数中的 NSLog 都没有出现在控制台中,这主要是我迷路的原因。即使我可以为用户提供一种自行关闭 webview 的方法,同时仍然触发 objc_authorizeWithCallbackURL 的成功块,那也没问题。我很感激任何建议,如果我应该提供更多代码/信息,请告诉我。谢谢!
编辑:根据评论将 webview 更改为 SFViewController。现在用户看到一个“完成”按钮,它将正确触发 safariViewControllerDidFinish 函数,但我仍然没有看到任何数据或任何与 OAuth2.0 进程相关的成功或失败。
【问题讨论】:
-
据我了解,由于存在漏洞,您不应将 webview 与 OAuth 2.0 一起使用。 WebView 是单进程的,因此渲染器引擎中的任何安全漏洞实际上都会授予任何恶意代码与您的应用程序相同的权限。你应该使用 SFSafariViewController。
-
@jwilleke 谢谢我改成了 SFSafariViewController 但问题仍然存在。
标签: ios objective-c oauth-2.0 fitbit