Foursquare API request not working once deployed, crossdomain issues? - actionscript-3

I threw together a simple app in flash ide / as3 that pulls photos from a foursquare location. It works locally but once deployed on the web it does not work. I would guess it's a crossdomain issue but what I've found online (link below) so far doesn't answer my question.
http://groups.google.com/group/foursquare-api/browse_thread/thread/ab963f74fde8ae9f/3e68559966acf7c9
I've checked the local vs network setting.I've read I shouldn't need a crossdomain file on my server.
And I believe I've read that I shouldn't need a php proxy for foursquare like I need with twitter...
Not sure what else it can be.
the box where it says testing should trace the response but it never fills, it's like the call stops dead in its tracks after it hits the crossdomain file on foursquare and I don't even get an error response. http://physicalgraffitea.com/wp-content/swf/Foursquare-e.swf
Distilled the AS3 down to it's leanest here, sans the ID and Secret.
Any help? Thanks
Security.allowDomain("http://www.foursquare.com");
Security.loadPolicyFile("http://api.foursquare.com/crossdomain.xml");
import flash.net.URLRequest;
import flash.net.URLLoader;
import com.adobe.serialization.json.JSON;
var resource = "https://api.foursquare.com/v2/venues/4b7071c8f964a520d51a2de3/photos?group=venue&limit=6&client_id="+myId+"&client_secret="+mySecret+"&v=20120517";
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest(resource);
request.method = URLRequestMethod.GET;
loader.addEventListener(Event.COMPLETE, onComplete);
loader.addEventListener(IOErrorEvent.IO_ERROR,onIOError);
loader.load(request);
function onComplete(e:Event):void {
var loader:URLLoader = URLLoader(e.target);
outputtxt.text = loader.data;
trace(loader.data);
var jsonData:Object = JSON.decode(loader.data)
}
function onIOError(e:Event):void {
var loader:URLLoader = URLLoader(e.target);
outputtxt.text = "error " + loader.data;
}

After a lot of fussing around the only way I could get it to work is to yes, use a crossdomain proxy php file with the foursquare api.
But that alone didn't solve my problem the swf needed to be referenced with the full http path but without the "www" in the path. "http://mydomain.com/swf/file.swf"
Not sure why but if you have similar cross domain issues try removing the www too

Related

Why am I receiving a flash player security error when trying to access facebook graph api photos?

I'm using the flash debug player(version 16) and I'm trying to access a users photo from within flash as3. Firstly, the Security error i'm receiving, here it is.
SecurityError: Error #2122: Security sandbox violation: Loader.content: http s://graffiti-galore.herokuapp.com/fb/GraffitiGalore.swf?build=0001 cannot access http s://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/c46.46.570.570/s50x50/398269_10152199084495525_2139734276_n.jpg?oh=29be37fd74f7d338a7b5c23b9aadd474&oe=5548F552&gda=1431395518_d142c304e8b8afa8df340e6cac093b29. A policy file is required, but the checkPolicyFile flag was not set when this media was loaded.
at flash.display::Loader/get content()
at PictureBox/onCompleteLoadingImage()
I'm using the free Heroku dino that is being offered and I'm simply doing a fetch call to the graph api users object. fetching the id,first_name, last_name,picture.
The picture url comes back as hosted on https facebook servers.
https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/c46.46.570.570/s50x50/398269_10152199084495525_2139734276_n.jpg?oh=29be37fd74f7d338a7b5c23b9aadd474&oe=5548F552&gda=1431395518_d142c304e8b8afa8df340e6cac093b29
Links like the one above. For some reason i'm receiving this Security error whenever loading up the image from my flash as3 facebook app.
Below is the code i'm using to fetch the image.
[code]
public function setupPhoto(url:String):void
{
removeAllChildren();
var urlRequest1:URLRequest = new URLRequest(url);
this.loader1 = new Loader();
this.loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadingImage);
this.loader1.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorLoadingImage);
this.loader1.load(urlRequest1);
//imageLoader = new ImageLoader(url);
//imageLoader.addEventListener(Event.COMPLETE, onCompleteLoadingImage);
//imageLoader.load();
}
private function onErrorLoadingImage(e:IOErrorEvent):void
{
debugPanel.writeLine("ERROR LOADING IMAGE!!!");
debugPanel.writeLine(e.text);
}
public function onCompleteLoadingImage(e:Event):void
{
//var sprite:Sprite = this.loader1.content as Sprite;
//updatePicture(sprite);
updatePicture1(this.loader1.content);
}
[/code]
this works for me with Facebook:
var lc:LoaderContext = new LoaderContext(true, null, null);
// now add the LoaderContext to your loader
this.loader1.load(urlRequest1, lc);
you don't have to create a LoaderContext every time, just create it once and use everywhere.

Getting Bitmap from Video decoded with Nestream AppendBytes (AS3)?

I am wondering if someone who has handled NetStream.appendBytes in Flash knows how to get the bitmapData from a decoded video frame? I have already looked at this question but that is from 3 years ago and the more recent comment/answer seems to be gone. In 2014 has anyone managed to turn those bytes into a bitmap? I am working with Flash Player 11.8 and this is not a desktop/AIR app.
In the image below I can do steps 1) and 2) but there's a brick wall at step 3)
The problem is that simply using bitmapdata.draw(video_container); does not work but instead it throws a Policy File error even though I am using a byteArray (from local video file in the same directory as the SWF). No internet is even involved but Flash tells me that "No Policy File granted permission from the server" or such nonsense. I think the error is just a bail-out insteading of straight up saying "You are not allowed to do this.."
I have tried: trying to appease this Crossdomain.xml issue anyway and looking into all known security/domain settings. I came to the conclusion that the error is not the problem but a side effect of the issue.. The issue here being that: Flash Player is aware of the SWF's location and of any files local to it. That's okay when you pass a String as URL etc but when the Netstream data is not local to the SWF domain then it becomes a Policy File issue. Problem is my data is in the Memory not in a folder like the SWF and therefore cannot alllow bitmapData.draw since it cannot "police" an array of bytes, any known fixes for this?... (I can't even say the words I really wanted to use).
What I am trying to achieve: Is to essentially use Netstream as an H.263 or H.264 image decoder in the same way Loader is a JPEG-to-Bitmap decoder or LoadCompressed.. is an MP3-to-PCM decoder. You know, access the raw material (here RGB pixels), apply some effects functions and then send to screen or save to disk.
I know it is a little late, but I think I found a solution for your problem.
So, to avoid the Security sandbox violation #2123 Error, you have just to do like this :
// ...
net_stream.play(null);
net_stream.play('');
// ...
Hope that can help.
I know this question is a couple months old, but I wanted to post the correct answer (because I just had this problem as well and other will too).
Correct answer:
It's a bug that has been open at adobe for almost 2 years
Link to the bug on Adobe
Work Around until the bug gets fixed (I am using this and it works great):
Workaround using Sprite and graphics
To take a snapshot from a video stream we don't need NetStream.appendBytes which inject data into a NetStream object.
For that we can use BitmapData.draw which has some security constraints. That's why in many times we get a flash security error. About that, Adobe said :
"... This method is supported over RTMP in Flash Player 9.0.115.0 and later and in Adobe AIR. You can control access to streams on Flash Media Server in a server-side script. For more information, see the Client.audioSampleAccess and Client.videoSampleAccess properties in Server-Side ActionScript Language Reference for Adobe Flash Media Server. If the source object and (in the case of a Sprite or MovieClip object) all of its child objects do not come from the same domain as the caller, or are not in a content that is accessible to the caller by having called the Security.allowDomain() method, a call to the draw() throws a SecurityError exception. This restriction does not apply to AIR content in the application security sandbox. ...".
For crossdomain file creation and some other security config for AMS server, you can take a look on this post : Crossdomain Video Snapshot - Fixing BitmapData.draw() Security Sandbox Violation.
After allowing our script to get data from our video stream, we can pass to the code.
I wrote a code that play a video stream ( rtmp or http ) and take a snapshot to show it in the stage or save it as a file after applying a pixel effect :
const server:String = null; //'rtmp://localhost/vod'
const stream:String = 'stream'; // 'mp4:big_buck_bunny_480p_h264.mp4';
var nc:NetConnection;
var ns:NetStream;
var video:Video;
const jpg_quality:int = 80;
const px_size:int = 10;
nc = new NetConnection();
nc.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent):void{});
nc.addEventListener(NetStatusEvent.NET_STATUS, function(e:NetStatusEvent):void{
if(e.info.code == 'NetConnection.Connect.Success'){
ns = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS, function(e:NetStatusEvent):void{});
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, function(e:AsyncErrorEvent):void{});
video = new Video(320, 180);
video.x = video.y = 10;
video.attachNetStream(ns);
addChild(video);
ns.play(stream);
}
})
nc.connect(server);
btn_show.addEventListener(
MouseEvent.CLICK,
function(e:MouseEvent): void{
var bmp:Bitmap = pixelate(video, px_size);
bmp.x = 10;
bmp.y = 220;
addChild(bmp);
}
)
btn_save.addEventListener(
MouseEvent.CLICK,
function(e:MouseEvent): void{
var bmp:Bitmap = pixelate(video, px_size);
var jpg_encoder:JPGEncoder = new JPGEncoder(80);
var jpg_stream:ByteArray = jpg_encoder.encode(bmp.bitmapData);
var file:FileReference = new FileReference();
file.save(jpg_stream, 'snapshot_'+int(ns.time)+'.jpg');
}
)
function pixelate(target:DisplayObject, px_size:uint):Bitmap {
var i:uint, j:uint = 0;
var s:uint = px_size;
var d:DisplayObject = target;
var w:uint = d.width;
var h:uint = d.height;
var bmd_src:BitmapData = new BitmapData(w, h);
bmd_src.draw(d);
var bmd_final:BitmapData = new BitmapData(w, h);
var rec:Rectangle = new Rectangle();
rec.width = rec.height = s;
for (i = 0; i < w; i += s){
for (j = 0; j < h; j += s){
rec.x = i;
rec.y = j;
bmd_final.fillRect(rec, bmd_src.getPixel32(i, j));
}
}
bmd_src.dispose();
bmd_src = null;
return new Bitmap(bmd_final);
}
Of course, this is just a simple example to show the manner to get a snapshot from a video stream, you should adapt and improve it to your needs ...
I hope all that can help you.

How to use httpget on AdobeAIR?

Using some home automation software that allows me to use httpget/httppost to get the status and control the state of devices in my home. I can use it just fine when I'm on my home wifi network (using the internal IP address in the URL), but when I attempt the same outside of my network, nothing happens. I have all ports forwarded properly, because if I post the exact same URL into my browser, it will perform whatever task was indicated.
An example would be:
function LampOn(event:MouseEvent):void{
import flash.net.*;
var url:String = "http://" + ipaddy + "/HomeSeer_REST_API.aspx?function=setdevicebyname&param1=lamp&param2=on";
var request:URLRequest = new URLRequest(url);
var loader:URLLoader = new URLLoader();
loader.load(request);
}
When I use my home IP in the variable 'ipaddy' it works fine, when I use my external IP (in the form username:password#ipaddress:port ) it does nothing.

AS3 using the loader CLASS with .content

I've been working on a program, but currently I'm stuck.
My problem is the .content statement in my script. Since I'm loading a PNG from the web I have the Local playback security set to "Access network only". When I load pictures locally (of course with settings at "Access local files only", and local URLRequests) the program works just fine, but when loading from the web the .content makes my program stop, I've found this out with lots of testing. It seems the .content only works when the SWF file only uses local requests, is this correct?
I know that I can use addChild(loader), but I need to get the PNG file out of the loader so that I can turn it into a bitmap and edit it. Any ideas? The script is below.
var loader:Loader = new Loader;
var ar:Array = [];
var teller:Number = 0;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, paste);
knapp.addEventListener(MouseEvent.CLICK, neste);
function paste(evt) {
ar[teller] = loader.content;
ar[teller].x = ar[teller].width*teller;
addChild(ar[teller]);
}
function neste(evt) {
teller ++
loader.load( new URLRequest ("http://www.minecraft.net/skin/Notch.png"));
}
loader.load( new URLRequest ("http://www.minecraft.net/skin/Notch.png"));
Cross-domain policies prohibits image data manipulation for images loaded from another domain than the one the swf file is loaded from:
http://www.dwuser.com/flashslideshow/v4/help/advanced_topics/understanding_flash_player_cross_domain_loading_restrictions.html

How to run an external SWF inside a Flex Application?

EDIT: Due to the answer I change the code posted. I've added the Security.allowDomain("*") line and that line throws me an error. So, how can that be made?
I want to run an Action Script 3.0 Application into a Flex Application. To do this I've done the following:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication windowComplete="loadSwfApplication()" xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
private function loadSwfApplication()
{
// The next line throws me an error.
Security.allowDomain("*");
var urlRequest:URLRequest = new URLRequest("path/to/the/application.swf");
swfLoader.addEventListener(Event.COMPLETE, loadComplete);
swfLoader.load(urlRequest);
}
private function loadComplete(completeEvent:Event)
{
var swfApplication:* = completeEvent.target.content;
swfApplication.init(); // this is a Function that I made it in the Root class of swfApplication
}
]]>
</mx:Script>
<mx:SWFLoader id="sfwLoader"/>
</mx:WindowedApplication>
The problem is that in the calling of swfApplication.init(); the AIR Player throws me an exception:
Security sandbox violation: caller file:///path/to/the/application.swf cannot access Stage owned by app:/SWFApplicationLoader.swf.
This is because somewhere in application.swf I use the stage like this:
if (root.stage != null)
root.stage.addEventListener(Event.REMOVED, someFunction);
root.stage.stageFocusRect = false;
How can I load this swf application and USE the stage without any problems?
You can try to load your SWF temporarily into a ByteArray and then load it with your SWFLoader.
Don't forget to set allowCodeImport to true since your SWF has as code inside.
Of course be sure that your loaded swf is secure enough for your application since it will have access at all your property.
private function loadSwfApplication():void {
// load the file with URLLoader into a bytearray
var loader:URLLoader=new URLLoader();
// binary format since it a SWF
loader.dataFormat=URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onSWFLoaded);
//load the file
loader.load(new URLRequest("path/to/the/application.swf"));
}
private function onSWFLoaded(e:Event):void {
// remove the event
var loader:URLLoader=URLLoader(e.target);
loader.removeEventListener(Event.COMPLETE, onSWFLoaded);
// add an Application context and allow bytecode execution
var context:LoaderContext=new LoaderContext();
context.allowCodeImport=true;
// set the new context on SWFLoader
sfwLoader.loaderContext = context;
sfwLoader.addEventListener(Event.COMPLETE, loadComplete);
// load the data from the bytearray
sfwLoader.load(loader.data);
}
// your load complete function
private function loadComplete(completeEvent:Event):void {
var swfApplication:* = completeEvent.target.content;
swfApplication.init(); // this is a Function that I made it in the Root
// class of swfApplication
}
If they are being loaded from different domains you are going to have to add a security exception - http://livedocs.adobe.com/flex/3/html/help.html?content=05B_Security_08.html
if its being run locally youre probably going to have to add them to the list of trusted files or folders in the settings manager - http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html#117502
Assuming that the external SWF is also in the application directory, you could try loading it using the app:/ scheme:
var urlRequest:URLRequest = new URLRequest("app:/path/application.swf");
That may put it into the same security context as the main application.
One thing you may want to consider is that if you are trying to run a SWF from inside your application directory in AIR, AIR restricts execution of files. If you copy the file to a temp file and the run it (along with allowLoadBytesCodeExecution set to true) then it works.
var file:File = File.applicationDirectory.resolvePath("myFile.swf");
this.tmpFile = File.createTempDirectory().resolvePath("myFile.swf");
file.copyTo(this.tmpFile);
imagePreview.loaderContext = lc;
imagePreview.source = tmpFile.url;
it won't work for Flex Projectors.
Only we use SWFLoader and LocalConnection because they can communicate between external swf and main swf. Thanks for support!
Can you read my tutorial from Adobe's Forum
It is very better than MovieClip or Object-Callers
Thank for resolved solution :)
Best regards, Jens