我想通過動作腳本3 Flash應用程序訪問Watson文本到語音API。正如您所知,Adobe實施了一項新的安全功能,以使用基於規則的xml配置文件(crossdomain.xml)的機制來限制跨域的訪問。在我的情況下,執行腳本時提出了以下錯誤:從Adobe動作腳本沃森API訪問3
的源代碼:
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); } } }
控制檯輸出:
[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.
¿存在另一種選擇,從動作腳本的Flash訪問API應用程序?
**選項1 **。一些服務考慮了Flash安全模型,並提供了使用其功能的方法。閱讀他們的文檔或聯繫他們的支持。 **選項2 **。如果您不需要它是基於Web的應用程序,請使用AIR。桌面/移動應用程序的限制較少。 **選項3 **。你可以隨時訴諸你的應用** <-> **你的服務器** <-> **他們的服務模式。 – Organis
顯示一些可以測試的代碼以重新創建此錯誤。也許有人可以修復它。 –
@Garrapato,PHP是一個訪問數據並傳遞給AS3的選項嗎?您的安全性錯誤是因爲您的網站是'http://',但您嘗試從'https://'網站加載媒體。即使你解決了這個問題(通過使用安全/ HTTPS服務器),你也會得到真正的錯誤:「Actionscript中不允許授權頭」。使用PHP或Javascript並通過外部接口將數據傳遞給AS3 .. –