Picasa Web Albums Data API

Developer's Guide: .NET

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 API 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.

Contents

Audience

This document is intended for programmers who want to write client applications using the Google Data API .NET client library that can interact with Picasa Web Albums.

Getting Started

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:

  1. Obtain or construct the appropriate URL.
  2. If you're sending data to a service (for example, if you're inserting a new entry), then transform the raw data into objects using the client library classes. (This step doesn't apply if you are merely retrieving data or uploading from a file.)
  3. Create a new PicasaService instance, setting your application's name (in the form companyName-applicationName-versionID).
  4. Set the appropriate credentials.
  5. Call a method to send the request and receive any results.

Authentication

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.

Single-user "installed" client authentication

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 service = new PicasaService("exampleCo-exampleApp-1");
service.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.

Multiple-user web application client authentication

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", 
    "https://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".

Working with Albums

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.

Request a list of albums

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.

Add an album

Albums can also be created using the API. If you want to create a new public 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";
AlbumAccessor ac = new AlbumAccessor(newEntry);
//set to "private" for a private album
ac.Access = "public";

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."

Modify the properties of 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();

Delete an album

Retrieved entries can also be deleted using the Delete method.

entry.Delete();

Working with Photos

When uploading, modifying, or removing photos, you will have to authenticate using one of the methods discussed in the Authentication section.

Request a list of photos

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.

List photos in album

This is the basic query to retrieve all of the photos belonging to username in the album corresponding to albumid. 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, albumid));

PicasaFeed feed = service.Query(query);

foreach (PicasaEntry entry in feed.Entries)
{
    Console.WriteLine(entry.Title.Text);
}

List photos recently uploaded

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);
}

List photos by community search

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("https://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);
}

Accessing photo information

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.

Post a new photo

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, albumid is the the id 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, albumid));

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("https://picasaweb.google.com/data/feed/api/user/default/albumid/default"), fileStream, "image/jpeg", file);

Update a photo

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();

Delete a photo

Deleting a photo is done by using the Delete method.

entry.Delete();

Working with Tags

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.

Retrieving a list of tags

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.

List tags by user

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);
}

List tags by album

The following code will print out all of the tags that username has tagged photos with in the album with id albumid.

TagQuery query = new TagQuery(PicasaQuery.CreatePicasaUri(username, albumid));
PicasaFeed feed = service.Query(query);

foreach (PicasaEntry entry in feed.Entries)
{
    Console.WriteLine(entry.Title.Text);
}

List tags by photo

The following code will print out all of the tags that username has tagged the photo identified by photoid with in the album with id albumid. The Accessing photo information section describes how to retrieve a valid value for photoid.

TagQuery query = new TagQuery(PicasaQuery.CreatePicasaUri(username, albumid, 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.

Search photos using tags

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.

Adding a tag to a photo

The following code adds the tag "foo" to the photo identified by photoid, in the album with id albumid which is owned by username.

Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumid, 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 from a photo

Deleting a tag is done by using the Delete method.

entry.Delete();

Working with Comments

Comments are short text snippets attached to photos by Picasa Web Albums users.

Retrieving a list of comments

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);
}

List comments by photo

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 album with id albumid, 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, albumid, photoid));
PicasaFeed feed = service.Query(query);

foreach (PicasaEntry entry in feed.Entries)
{
    Console.WriteLine(entry.Title.Text + ": " + entry.Content.Content);
}

Add comments to a photo

The following code adds the comment "great photo!" to the photo identified by photoid in the album with id albumid owned by username.

Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username, albumid, photoid));

CommentEntry entry = new CommentEntry();
newEntry.Content.Content = "great photo!";

PicasaEntry createdEntry = (PicasaEntry) service.Insert(postUri, entry);

Delete a comment from a photo

Deleting a comment is done by using the Delete method.

entry.Delete();

Working with Contacts

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.

Retrieving a list of contacts

To retrieve a user's list of contacts, you have to retrieve the following feed:

https://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("https://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);
}

 

Back to top

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.