我正在开发与思科Webex Teams Api集成的应用程序。不幸的是,对于macOS,他们没有SDK(只有iOS的SDK),所以我试图通过“手动”方式对其Api进行身份验证。
因此,我使用的是自定义URL,该URL具有备份的客户ID来检索代码。呼叫此URL时,Cisco的常规登录过程开始,要求用户使用其用户名登录并输入密码。成功登录后,Cisco的服务器会为您提供一个URL,其中包含随后需要进行编码的代码。
自定义网址
我的原型当前仅包含一个按钮,该按钮调用自定义URL进行身份验证。要获取此URL,我需要在此处注册与Cisco的集成:https : //developer.webex.com/my-apps
到目前为止,当我单击按钮时,WKWebView实例将接管我的自定义URL:
https://api.ciscospark.com/v1/authorize?client_id=<**personalClientId**>&response_type=code&redirect_uri=<**BundleIdentifier**>3A%2F%2Fredirect&scope=spark%3Aall%20spark%3Akms&state=set_state_here
重定向URI
因此,我的重定向uri当前是ch.appfros.webexoauthwokflowprototype:// redirect;此重定向uri已在我的集成注册表中向Cisco注册。
我知道我必须将此重定向uri放入我的应用程序中,所以我这样做了,导致相应的info.plist
部分如下所示:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>response</string>
<key>CFBundleURLSchemes</key>
<array>
<string>ch.appfros.webexoauthwokflowprototype://</string>
</array>
</dict>
</array>
准备 AppDelegate
据我目前所知,似乎还需要在我的中有一个附加函数AppDelegate
来处理回调。对于cocoa
,这似乎是func application(_ application: NSApplication, open urls: [URL])
方法。
据我了解,我将不得不在那里处理所有重定向。我AppDelegate
目前看起来像这样:
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func application(_ application: NSApplication, open urls: [URL]) {
print("got answer")
}
}
我的问题是,执行登录过程会导致出现消息 There is no application set to open the URL ch.appfros.webexoauthwokflowprototype://redirect?code=
&state=loggedIn.
因此,Cisco端的身份验证过程成功完成,它为我的系统提供了URL-我的计算机不知道该如何处理...我什至可以看到我需要检索即将到来的URL中的访问的代码返回,我只是没有将它们包含在我的应用程序中才能继续...
很明显,我在这里遗漏了一些关键的东西-我只是不知道它是什么,而且在线研究对我没有帮助。我可以在这里和那里找到点点滴滴,但是我仍然要找到一个简明的说明,说明我该如何为macOS应用程序做些什么。
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application)
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleAppleEvent(event:replyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func application(_ application: NSApplication, open urls: [URL]) {
print("got answer")
}
@objc
func handleAppleEvent(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
print("got answer in event handler")
}
}
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application)
}
func applicationWillFinishLaunching(_ notification: Notification) {
NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(self.handleAppleEvent(event:replyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func application(_ application: NSApplication, open urls: [URL]) {
print("got answer")
}
@objc
func handleAppleEvent(event: NSAppleEventDescriptor, replyEvent: NSAppleEventDescriptor) {
print("got answer in event handler")
}
}
事实证明,沙盒确实是问题所在。禁用沙箱功能后-我的公司将成为内部帮助的小助手,没什么花哨的东西,对于任何商店来说肯定没有任何东西-它立即起作用。
如果需要的话,我将不得不研究NSAppTransportSecurity
实现的方式-感谢@NoHalfBits将我推向正确的方向。
所以现在我有
func application(_ application: NSApplication, open urls: [URL])
在我的AppDelegate中实现(事实证明,这确实足够了,就像@Aaron Raimist所说的那样谢谢你的帮助; 希望这可以帮助其他人(或我未来的自我):-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句