Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从adobe action script 3访问Watson API_Api_Security_Actionscript 3_Flash_Watson - Fatal编程技术网

从adobe action script 3访问Watson API

从adobe action script 3访问Watson API,api,security,actionscript-3,flash,watson,Api,Security,Actionscript 3,Flash,Watson,我正在尝试通过ActionScript3Flash应用程序访问Watson文本到语音API。正如您所知,Adobe使用基于规则的xml配置文件(crossdomain.xml)的机制实现了一种新的安全功能,以限制跨域访问。在我的情况下,执行脚本时会出现以下错误: 源代码: package { import flash.net.URLRequest; import flash.net.URLRequestHeader; import flas

我正在尝试通过ActionScript3Flash应用程序访问Watson文本到语音API。正如您所知,Adobe使用基于规则的xml配置文件(crossdomain.xml)的机制实现了一种新的安全功能,以限制跨域访问。在我的情况下,执行脚本时会出现以下错误:

源代码:

package { import flash.net.URLRequest; import flash.net.URLRequestHeader; import flash.net.URLLoaderDataFormat; import flash.net.URLLoader; import flash.net.URLVariables; import flash.net.URLRequestMethod; import flash.events.Event; import flash.events.HTTPStatusEvent; import flash.events.SecurityErrorEvent; import flash.events.IOErrorEvent; public class Greeter { public function sayHello():String { 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 = "application/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); try { postLoader.load(request); } catch (error:Error) { trace("Unable to load post URL"); } var greeting:String; greeting = "Prueba de conexión a Watson!"; return JSON.stringify(request.data); } private function loaderCompleteHandler(event:Event):void { trace("loaderCompleteHandler: "); } private function httpStatusHandler(event:HTTPStatusEvent):void { trace("httpStatusHandler: "); } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } } } 包裹 { 导入flash.net.URLRequest; 导入flash.net.URLRequestHeader; 导入flash.net.URLLoaderDataFormat; 导入flash.net.urloader; 导入flash.net.url变量; 导入flash.net.URLRequestMethod; 导入flash.events.Event; 导入flash.events.HTTPStatusEvent; 导入flash.events.SecurityErrorEvent; 导入flash.events.IOErrorEvent; 公务舱迎宾员 { 公共函数sayHello():字符串 { var params:Object={用户:“John”,密码:“secret”}; var-request:URLRequest=新的URLRequest(); request.url=”https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices"; request.contentType=“application/json”; request.method=URLRequestMethod.POST; request.data=JSON.stringify(params); var contentTypeHeader:URLRequestHeader=新的URLRequestHeader(“内容类型”、“应用程序/json”); var acceptHeader:URLRequestHeader=新的URLRequestHeader(“接受”、“应用程序/json”); var formDataHeader:URLRequestHeader=新的URLRequestHeader(“内容类型”、“应用程序/json”); var authorizationHeader:URLRequestHeader=新的URLRequestHeader(“授权”,“基本YJCXYWUWNTTZTJMYI00ZMQZLWFIMTCTOTRJYTC2MZYZYWE3OLZ5DU9VZ0W3AK1ZVW=”; request.requestHeaders=[acceptHeader,formDataHeader,authorizationHeader,contentTypeHeader]; var postLoader:URLLoader=新的URLLoader(); postLoader.dataFormat=URLLoaderDataFormat.BINARY; postLoader.addEventListener(Event.COMPLETE,loaderCompleteHandler); postLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler); postLoader.addEventListener(SecurityErrorEvent.SECURITY\u ERROR,securityErrorHandler); postLoader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); 尝试 { 加载(请求); } 捕获(错误:错误) { 跟踪(“无法加载帖子URL”); } 变量:字符串; 问候语=“Prueba de conexión a Watson!”; 返回JSON.stringify(request.data); } 私有函数loaderCompleteHandler(事件:event):void { 跟踪(“loaderCompleteHandler:”); } 私有函数httpStatusHandler(事件:HTTPStatusEvent):无效 { 跟踪(“httpStatusHandler:”); } 私有函数securityErrorHandler(事件:SecurityErrorEvent):void { 跟踪(“securityErrorHandler:+事件”); } 私有函数ioErrorHandler(事件:IOErrorEvent):void { 跟踪(“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 conexió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 [trace] 05:45:44 PM | err | [SecurityErrorEvent type="securityError" bubbles=false cancelable=false eventPhase=2 text="Error #2170: Security sandbox violation: http://garragames.com/garra-x/Tick.swf cannot send HTTP headers to https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices."] [trace] Error #2044: Unhandled securityError:. text=Error #2170: Security sandbox violation: http://garragames.com/garra-x/Tick.swf cannot send HTTP headers to https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices. [跟踪]广告:cargar el archivo de política desde错误https://watson-api-explorer.mybluemix.net/crossdomain.xml [trace]***Violación de la seguridad沙箱*** [追踪]这是康涅西翁监狱的一个秘密https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices -无硒许可证http://garragames.com/garra-x/Tick.swf [trace]05:45:44 PM | err |[SecurityErrorEvent type=“securityError”气泡=false cancelable=false eventPhase=2 text=“Error#2170:安全沙盒冲突:http://garragames.com/garra-x/Tick.swf 无法将HTTP头发送到https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices."] [跟踪]错误#2044:未处理的安全性错误:。text=错误#2170:安全沙盒冲突:http://garragames.com/garra-x/Tick.swf 无法将HTTP头发送到https://watson-api-explorer.mybluemix.net/text-to-speech/api/v1/voices.
?是否存在从Action Script Flash App访问API的另一个选项?

您真正的问题应该是“如何使用Flash的Watson API进行身份验证?”,而不是如何通过
URLLoader
(具有自动跨域检查)克服加载/解码的安全沙盒问题

您必须以某种方式进行身份验证(登录)。这似乎不太可能仅通过Actionscript实现。您可以看到Flash Player错误,如:

"Authorization header cannot be sent using Actionscript"
通过使用
URLStream
代替
urloader
。而且
URLStream
不关心安全问题。如果字节存在,它只获取字节。据报道,Flash“授权”请求是允许的。但对我来说不起作用。也许调试器中不允许这样做

一旦从您的URL/域进行了身份验证,那么您的Flash应用程序也可以像正常的
POST
URL一样发出任何请求,因为它是通过相同的(现在允许的)域发出请求的。如果需要字节,请使用
URLStream
而不是
urloader
,因为它没有跨域限制

PS:例如,您可以使用
声音
对象来播放转换为语音的文本

(如果
已验证
,即:您已登录)请尝试:

  • 在舞台上制作一个
    输入
    文本框,实例名为txtbox
  • 将下面的代码保存在名为:
    Main.as
    (编译为Main.swf)的文档类中
测试下面的代码:(SWF result=在文本框中键入,然后按enter键收听语音)

这仅在SWF文件嵌入HTML页面时才起作用。如下所示:

    <!DOCTYPE html>
    <html>
    <body>

    <audio id="audio_watson">
    <source src="http://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/mp3&text=welcome&voice=en-US_AllisonVoice" type="audio/mpeg">
    </audio>

    <embed src="Main.swf" width="800" height="600">

    <script>
    var a = document.getElementById("audio_watson");
    a.play(); //playback to trigger authentication
    </script>

    </body>
    </html>

var a=document.getElementById(“audio_watson”);
a、 play()//播放以触发身份验证
您的
    <!DOCTYPE html>
    <html>
    <body>

    <audio id="audio_watson">
    <source src="http://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/mp3&text=welcome&voice=en-US_AllisonVoice" type="audio/mpeg">
    </audio>

    <embed src="Main.swf" width="800" height="600">

    <script>
    var a = document.getElementById("audio_watson");
    a.play(); //playback to trigger authentication
    </script>

    </body>
    </html>