【发布时间】:2018-03-16 11:55:40
【问题描述】:
我正在尝试通过操作脚本 3 flash 应用程序访问 Watson Text to Speech API。如您所知,Adobe 使用基于规则的 xml 配置文件 (crossdomain.xml) 的机制实施了一项新的安全功能,以限制跨域的访问。在我的情况下,执行脚本时会引发以下错误:
源代码:
包裹 { 导入 flash.net.URLRequest; 导入 flash.net.URLRequestHeader; 导入 flash.net.URLLoaderDataFormat; 导入 flash.net.URLLoader; 导入 flash.net.URLVariables; 导入 flash.net.URLRequestMethod; 导入 flash.events.Event; 导入 flash.events.HTTPStatusEvent; 导入 flash.events.SecurityErrorEvent; 导入 flash.events.IOErrorEvent; 公共课迎宾员 { 公共函数sayHello():字符串 { var params:Object = {user:"John",password:"secret"}; var request:URLRequest = new URLRequest(); request.url = "https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices"; request.contentType = "应用程序/json"; request.method = URLRequestMethod.POST; request.data = JSON.stringify(params); var contentTypeHeader:URLRequestHeader = new URLRequestHeader("Content-Type","application/json"); var acceptHeader:URLRequestHeader = new URLRequestHeader("Accept","application/json"); var formDataHeader:URLRequestHeader = new URLRequestHeader("Content-Type","application/json"); var authorizationHeader:URLRequestHeader = new URLRequestHeader("Authorization","Basic YjcxYWUwNTMtZTJmYi00ZmQzLWFiMTctOTRjYTc2MzYzYWE3OlZ5dU9VZ0w3ak1zVw=="); request.requestHeaders = [acceptHeader,formDataHeader,authorizationHeader,contentTypeHeader]; var postLoader:URLLoader = new URLLoader(); postLoader.dataFormat = URLLoaderDataFormat.BINARY; postLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler); postLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler); postLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); postLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 尝试 { postLoader.load(请求); } 捕捉(错误:错误) { trace("无法加载帖子 URL"); } 变量问候:字符串; Greeting = "Prueba de conexión a Watson!"; 返回 JSON.stringify(request.data); } 私有函数 loaderCompleteHandler(event:Event):void { trace("loaderCompleteHandler:"); } 私有函数 httpStatusHandler(event:HTTPStatusEvent):void { trace("httpStatusHandler:"); } 私有函数securityErrorHandler(事件:SecurityErrorEvent):无效 { trace("securityErrorHandler:" + event); } 私有函数 ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler:" + 事件); } } }控制台输出:
[trace] Advertencia: Error al cargar el archivo de política desde https://watson-api-explorer.mybluemix.net/crossdomain.xml [trace] *** Violación de la seguridad Sandbox *** [trace] Se ha detenido la conxión con https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices - no se permite desde http://garragames.com/garra-x /tick.swf [追踪] 05:45:44 PM |错误 | [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="错误 #2170: 安全沙箱违规:http://garragames.com/garra-x/Tick.swf 无法将 HTTP 标头发送到 https:// /watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices."] [trace] 错误 #2044:未处理的安全错误:。 text=Error #2170:违反安全沙箱:http://garragames.com/garra-x/Tick.swf 无法将 HTTP 标头发送到 https://watson-api-explorer.mybluemix.net/text-to-speech/ api/v1/声音。¿是否存在从 Action Script Flash 应用程序访问 API 的其他选项?
【问题讨论】:
-
选项 1。一些服务考虑到了 Flash 安全模型,并提供了使用其功能的方法。阅读他们的文档或联系他们的支持。 选项 2。如果您不需要它是基于 Web 的应用程序,请使用 AIR。桌面/移动应用程序的限制较少。 选项 3。你总是可以求助于你的应用你的服务器他们的服务模型。
-
显示一些可以测试重新创建此错误的代码。也许有人可以解决它。
-
@Garrapato,PHP 是访问数据并传递给 AS3 的选项吗?您的安全错误是因为您的站点是
http://,但您尝试从https://站点加载媒体。即使你修复了这个问题(通过使用安全/HTTPS 服务器),你也会得到真正的错误:Authorization header is not allowed in Actionscript。使用 PHP 或 Javascript 并通过外部接口将数据传递给 AS3.. -
我将使用https协议进行测试,但是根据adobe文档,IBM Watson Team应该在根目录中放置一个带有规则的crossdomain.xml文件,以允许从我的域进行远程访问。跨度>
-
我刚刚尝试使用来自 garragames.com 的 https 协议,结果相同:
[trace] Advertencia: Error al cargar el archivo de política desde watson-api-explorer.mybluemix.net/crossdomain.xml [trace] ** * Violación de la seguridad Sandbox *** [trace] Se ha detenido la conxión con watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices/… - no se permite desde garragames.com/garra-x/Tick.swf
标签: api security actionscript-3 flash watson