Picasa Web Albums allows client applications to view and update albums, photos, and comments in the form of Google Data API feeds. Your client application can use the Picasa Web Albums Data API to create new albums, upload photos, add comments, edit or delete existing albums, photos, and comments, and query for items that match particular criteria.
In addition to providing some background on the capabilities of the Picasa Web Albums Data API, this guide provides examples for interacting with the API using the .NET Google Data Client Library. For help setting up the client library, see the Getting Started Guide.
If you're interested in understanding more about the underlying protocol used by the .NET client library to interact with the Picasa Web Albums API, please see the protocol guide.
This document is intended for programmers who want to write client applications using the Google Data .NET client library that can interact with Picasa Web Albums.
Picasa Web Albums uses Google Accounts for authentication, so if you have a Google account you are all set. Otherwise, you can create a new account.
For help setting up the client library, see the Getting Started Guide. To use the .NET client library, you'll need the .NET 1.1 runtime, and you should also be current on all patches. After downloading the client library, you'll find the DLLs you need to get started in the lib/Release subdirectory of the distribution.
To compile the examples in this document into your own code, you'll need the following using statements:
using Google.GData.Photos; using Google.GData.Client; using Google.GData.Extensions; using Google.GData.Extensions.Location;
The PicasaService class represents a client connection (with authentication) to the Picasa Web Albums web service. The general procedure for sending a query to a service using the client library consists of the following steps:
PicasaService instance, setting your application's name (in the form companyName-applicationName-versionID).The .NET client library can be used to work with either public or private feeds. Public feeds are read-only, but do not require any authentication. Private feeds require that you authenticate to the Picasa Web Albums servers. This can be done via ClientLogin username/password authentication or AuthSub proxy authentication.
Please see the Google Data APIs authentication documentation for more information on AuthSub and ClientLogin.
To use ClientLogin (also called "Authentication for Installed Applications"), invoke the setUserCredentials method of PicasaService, specifying the ID and password of the user on whose behalf your client is sending the query. For example:
PicasaService myService = new PicasaService("exampleCo-exampleApp-1");
myService.setUserCredentials("jo@gmail.com", "mypassword");
Once the credentials have been set, they will be used to authenticate each request made using this service object.
For more information about Google's authentication systems, see the Google Account Authentication documentation.
The AuthSub login protocol allows your web-based application to have a user authenticate through Google's servers. To familiarize yourself with how this works in .NET, please read this article on the basics of using AuthSub in the .NET client library.
Having reviewed that documentation, you can now make a few substitutions and use it to access the Picasa Web Albums API:
authSubUrl = AuthSubUtil.getRequestUrl("http://www.example.com/Hello.asp",
"http://picasaweb.google.com/data/", false, true);
Here we request a token with the proper scope to upload and modify photos and albums.
GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("lh2",
"exampleCo-exampleApp-1");
authFactory.Token = (String) Session["token"];
PicasaService service = new PicasaService(authFactory.ApplicationName);
service.RequestFactory = authFactory;
Note that the service name for the Picasa Web Albums API is "lh2".
Albums are the way Picasa Web Albums groups photos into useful sets. These albums can be public or unlisted, and have their own properties such as a geographic location, a description, or a date.
You do not have to authenticate to retrieve data about public albums, but in order to create, update, or delete content you must authenticate using one of the methods discussed in the authentication section.
You can retrieve all of a user's albums by using the AlbumQuery class.
The string "default" can be used in place of a real username, in which case the server will use the username of the user credentials used to authenticate the request.
AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(username));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
AlbumAccessor ac = new AlbumAccessor(entry);
Console.WriteLine(ac.NumPhotos);
}
The above code retrieves all of the user's albums and prints out a list of them. It also prints the number of photos inside of each album.
Albums can also be created using the API. If you want to create a new album for username, then you can use the following:
AlbumEntry newEntry = new AlbumEntry(); newEntry.Title.Text = "New album"; newEntry.Summary.Text = "This is an album"; Uri feedUri = new Uri(PicasaQuery.CreatePicasaUri(username)); PicasaEntry createdEntry = (PicasaEntry) service.Insert(feedUri, newEntry);
This code will create a new album named "New album" with the description of "This is an album."
Once an entry is retrieved, it can be easily modified using the Update method.
entry.Title.Text = "new album title"; PicasaEntry updatedEntry = (PicasaEntry) entry.Update();
Retrieved entries can also be deleted using the Delete method.
entry.Delete();
When uploading, modifying, or removing photos, you will have to authenticate using one of the methods discussed in the Authentication section.
There are different ways to retrieve photos. The most common is to get a list of all of the photos in an album, but you can also retrieve recent photos from a user, or search photos from the public albums of other users.
This is the basic query to retrieve all of the photos belonging to username in the albumname album. The title of each photo is then printed out to the console.
The string "default" can be used in place of a real username, in which case the server will use the username of the user credentials used to authenticate the request.
PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(username, albumname));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
It is also possible to retrieve the photos associated with a user, but without specifying any particular album. The following example retrieves the last 10 photos uploaded by username and prints their titles.
PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(username));
query.NumberToRetrieve = 10;
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
With the API, you can search photos uploaded by other users, as long as they are in a public album. The following code retrieves 10 photos matching a search for "puppy" and prints out their titles.
PhotoQuery query = new PhotoQuery("http://picasaweb.google.com/data/feed/api/all");
query.Query = "puppy";
query.NumberToRetrieve = 10;
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
In previous examples, only the title of a photo was printed out. However, there is a lot of different data that can be retrieved about a photo such as EXIF information, thumbnail URLs, the ID of the album it is a part of, and more.
PhotoAccessor ac = new PhotoAccessor(entry);
string albumId = ac.AlbumId;
string photoId = ac.Id;
string camera = "";
if (entry.Exif.Make != null && entry.Exif.Model != null)
{
camera = entry.Exif.Make.Value + " " + entry.Exif.Model.Value;
}
string contentUrl = entry.Media.Content.Attributes["url"] as string;
//grab the URL of the first thumbnail
string firstThumbUrl = entry.Media.Thumbnails[0].Attributes["url"] as string;
Console.WriteLine("AlbumID: " + albumId);
Console.WriteLine("PhotoID: " + photoId);
Console.WriteLine("Camera: " + camera);
Console.WriteLine("Content URL: " + contentUrl);
Console.WriteLine("First Thumbnail: " + firstThumbUrl);
The above example prints out different pieces of information from a PicasaEntry. It also demonstrates how a PhotoAccessor can be used to retrieve elements from the gphotos namespace. The thumbnail and image URLs are taken from the MediaRSS elements inside of the entry. The camera data, if available, is found inside of the exif namespace.
You can upload a photo to Picasa Web Albums using the Insert method of the PicasaService object. The following demonstrates an example with a few assumptions: username is the name of the user who owns the album and that we have authenticated as, albumname is the the name of the album to upload the photo to, file is the path to the file on the local disk, service is an authenticated PicasaService object, and "image/jpeg" is the valid MIME type of the file to be uploaded.
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumname)); System.IO.FileInfo fileInfo = new System.IO.FileInfo(file); System.IO.FileStream fileStream = fileInfo.OpenRead(); PicasaEntry entry = (PicasaEntry) service.Insert(postUri, fileStream, "image/jpeg", file); fileStream.Close();
Notice that you did not specify a photo caption, tags, or any other sort of meta-data about the photo when it was uploaded. To do this, you have to update the entry you received back from the server, which leads into the next section.
If you want to post a photo, but don't want the hassle of requiring the user of your app to choose an album, you can post the photo to the 'Drop Box.' This special album will automatically be created the first time it is used to store a photo. To post to the 'Drop Box,' use a an albumid value of 'default'. In the .NET client library, your code would look like:
PicasaEntry entry = (PicasaEntry) service.Insert(
new Uri("http://picasaweb.google.com/data/feed/api/user/default/albumid/default"), fileStream, "image/jpeg", file);
Usually, you want to set up some meta-data when you are uploading photos. This example will give the photo a title, a caption, some tags, and geographic location. Notice that we are adding three tags (foo, bar, and baz) all at the same time using the <media:keywords> element.
entry.Title.Text = "New Photo"; entry.Summary.Text = "Photo caption"; entry.Media.Keywords.Value = "foo, bar, baz"; entry.Location = new GeoRssWhere(); entry.Location.Latitude = 37; entry.Location.Longitude = -122; PicasaEntry updatedEntry = (PicasaEntry) entry.Update();
Deleting a photo is done by using the Delete method.
entry.Delete();
Tags are a convenient way to label and organize your photos. By associating photos with descriptive strings, it makes searching through large quantities of photos easier.
Your program can retrieve a list of tags that are used by a user, in a particular album, or that are associated with a particular photo.
The following code will print out all of the tags that username has used in photos in their albums.
The string "default" can be used in place of a real username, in which case the server will use the username of the user credentials used to authenticate the request.
TagQuery query = new TagQuery(PicasaQuery.CreatePicasaUri(username));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
The following code will print out all of the tags that username has tagged photos with in the albumname album.
TagQuery query = new TagQuery(PicasaQuery.CreatePicasaUri(username, albumname));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
The following code will print out all of the tags that username has tagged the photo identified by photoid with in the albumname album. The Accessing photo information section describes how to retrieve a valid value for photoid.
TagQuery query = new TagQuery(PicasaQuery.CreatePicasaUri(username, albumname, photoid));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
Note that this same information is available inside of the <media:keywords> element of the photo itself in a comma separated format.
The following code uses the tag parameter to search for all photos belonging to username that are tagged with both "foo" and "bar".
PhotoQuery query = new PhotoQuery(PicasaQuery.CreatePicasaUri(username));
query.Tags = "foo,bar";
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text);
}
Note you could search for photos in a particular album by adding the album name as an additional parameter to CreatePicasaUri.
The following code adds the tag "foo" to the photo identified by photoid, in the albumname album which is owned by username.
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumname, photoid)); TagEntry newEntry = new TagEntry(); newEntry.Title.Text = "foo"; PicasaEntry createdEntry = (PicasaEntry) service.Insert(postUri, newEntry);
Note that this can also be done in bulk as described in the Update a photo section using the <media:keywords> element.
Deleting a tag is done by using the Delete method.
entry.Delete();
Comments are short text snippets attached to photos by Picasa Web Albums users.
The following example prints out the 10 most recent comments on a username's photos.
The string "default" can be used in place of a real username, in which case the server will use the username of the user credentials used to authenticate the request.
CommentsQuery query = new CommentsQuery(PicasaQuery.CreatePicasaUri(username));
query.NumberToRetrieve = 10;
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text + ": " + entry.Content.Content);
}
You can also retrieve all of the comments associated with a particular photo. The following example prints out all of the comments on the photo identified by photoid, inside of the albumname album, owned by the username user. The Accessing photo information section describes how to retrieve a valid value for photoid.
CommentsQuery query = new CommentsQuery(PicasaQuery.CreatePicasaUri(username, albumname, photoid));
PicasaFeed feed = service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
Console.WriteLine(entry.Title.Text + ": " + entry.Content.Content);
}
The following code adds the comment "great photo!" to the photo identified by photoid in the albumname owned by username.
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumname, photoid)); CommentEntry entry = new CommentEntry(); newEntry.Content.Content = "great photo!"; PicasaEntry createdEntry = (PicasaEntry) service.Insert(postUri, entry);
Deleting a comment is done by using the Delete method.
entry.Delete();
Contacts are also referred to as "favorite users" in the Picasa Web Albums interface. Currently the contacts feed in the API is read-only, so authentication is not required and you cannot modify a user's contacts.
To retrieve a user's list of contacts, you have to retrieve the following feed:
http://picasaweb.google.com/data/feed/api/user/username/contacts
The following code retrieves a user's list of contacts and some information about each contact:
PicasaQuery query = new PicasaQuery("http://picasaweb.google.com/data/feed/api/user/" + username + "/contacts");
query.KindParameter = "user";
PicasaFeed feed = (PicasaFeed) service.Query(query);
foreach (PicasaEntry entry in feed.Entries)
{
//many properties are in the Atom entry itself
Console.WriteLine(entry.Title.Text);
Console.WriteLine(entry.Authors[0].Name);
Console.WriteLine(entry.Authors[0].Uri.Content);
//but they are also in the gphoto namespace as well
Console.WriteLine(entry.getPhotoExtension("user").Value);
Console.WriteLine(entry.getPhotoExtension("nickname").Value);
Console.WriteLine(entry.getPhotoExtension("thumbnail").Value);
}