Accessibility

Table of Contents

Flickr Floater: Building an Adobe AIR application with Flex 3

Uploading files to Flickr

The upload process for Flickr is well documented. You could start uploading files immediately in Flex 3, using the ActionScript File.upload method, if you really wanted to. However, Flickr requires additional information passed with that call for authentication so that the application can be permitted to work within the Flickr API.

That authentication process has been described previously: the application key supplied with the shared secret to the Flickr page to receive user permission, which generates an authorization token. The call that the application will make needs to include the authentication details along with the file that it's uploading. That will require dynamically constructing a URL that includes authentication details to pass during the upload.

The Flickr ActionScript 3.0 library that I've referred to previously simplifies this process. It has methods to take care of assembling the correct URL required and perform the upload as well. The sample code below illustrates just how simple that is. The uploadPhoto function uses the value of the currently selected item in the FileSystemList component used to display list of available images. The function then gets the various authentication values from the settings object and uploads the file.

import com.adobe.webapis.flickr.events.*;
import com.adobe.webapis.flickr.FlickrService;
import com.adobe.webapis.flickr.methodgroups.Upload;
private function uploadPhoto():void {
 file = File.applicationStorageDirectory.resolvePath(imageList.selectedPath);
 file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,
  uploadCompleteHandler);
 var service:FlickrService = new
  FlickrService(_settings.apiKey);
 service.secret = _settings.secret;
 service.token = _settings.authToken;
 var uploader:Upload = new Upload(service);
 uploader.upload(file);
}

Uploading files to Flickr is pretty straightforward.

Figure 4. Uploading files to Flickr is pretty straightforward.

Note: I'm picking the bits of code that best illustrate the code and concepts here. However, often these bits are but a snapshot of what's possible. Think about the sample above and the first thing that should come to mind is that it should also be checking that the user has selected an image before clicking the upload button. I've skipped a lot of that kind of stuff in this article, so that I can show you the code needed just to make the application work. Please take the time to go through the actual code to see what else is in there.

The Flickr API will respond to a successful upload by returning an XML packet containing the Flickr ID for the image. That ID should be stored in the images database record and subsequently used by the event listener to upload user created details from the record to Flickr through another call to the API.

The Flickr API offers a number of methods that can be used to retrieve statistical information about your account that you could use within your application. For example, a call to the photos.getInfo method would retrieve the information for a specified image that would include details such as:

  • The number of views
  • The date posted
  • The number of comments
  • Tags
  • URL

The following snippet demonstrates how to call the photos.GetInfo Flickr API function using the Flickr ActionScript 3.0 library:

private function doFlickr(photoID:String):void {
 var service:FlickrService = new FlickrService(_settings.apiKey);
 service.secret = _settings.secret;
 service.addEventListener(FlickrResultEvent.PHOTOS_GET_INFO,
  onPhotoInfo);
 service.photos.getInfo(photoID);
}

One thing that took a bit of trial and error was dealing with calls to Flickr API methods that required authentication with write permission to update data entries, modifying the images details, for example. What's confusing is that when the user grants permission to the application to connect to their Flickr account they give it write permission, which means that it's permitted to read, write, and delete entries in the user's account. I eventually discovered that I could pass a permission argument as part of the call to the FlickrService, as shown in the following sample code:

public function setPhotoDetails(photoId:String,title:String,description:String):void
    {
 var service:FlickrService = new
  FlickrService(_settings.apiKey);
 service.secret = _settings.secret;
 service.token = _settings.authToken;
 service.permission = "write";
 service.addEventListener(FlickrResultEvent.PHOTOS_SET_META,
  setMetaResult);
 service.photos.setMeta(photoId,title,description);

The Flickr ActionScript 3.0 library offers support for all these methods, but you should be aware that the current SWC version of the code does not include a working version of the upload code, which you'll need for this application. This was not completely tested when the SWC was compiled for distribution; the library has been updated to include a working version. You will need to use the library of classes in a more conventional fashion. The library also needs the ActionScript 3.0 CoreLib utilities library in order to work as some of the utilities are used during its operations.