Wednesday, January 8, 2014

GetAlbums Picasa API 2.0 Using .NET


Here is a snippet of code from MyBlogger app that gets the list of public Picasa albums in .NET using the Picasa RESTFUL APIs V2.
       
       
public string CreatePublicGalleryUrlRequest(string accessToken)



{
 
string UrlRequest = "https://picasaweb.google.com/data/feed/api/user/default"

+ "?alt=json&v=2&kind=album&access=public&access_token=" + accessToken;

return (UrlRequest); // alt=json&v=2&



}



public async Task GetAlbums(string requestUrl)

{

    try

    {

        AlbumList = new ObservableCollection<AlbumNode>();

               

        // Add the default album location per Picasa documentation

        AlbumNode albumNode = new AlbumNode();

        albumNode.UploadUrl = "https://picasaweb.google.com/data/feed/api/user/default/albumid/default";

        albumNode.FriendlyUrl = albumNode.UploadUrl;

 

        AlbumList.Add(albumNode);

 

        HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

        request.Method = "GET";

 

        using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)

        {

            if (response.StatusCode != HttpStatusCode.OK)

                throw new Exception(String.Format(

                "Server error (HTTP {0}: {1}).",

                response.StatusCode,

                response.StatusDescription));

            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));

 

            object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());

            Response jsonResponse = objResponse as Response;

 

            if (jsonResponse != null)

            {

                if (jsonResponse.Feed.Entry != null)

                {
                                // Do your thing here
                }

            }

        }

    }

    catch (Exception e)

    {

        string message = string.Format("I am having trouble getting list of picassa public album: {0}", e.ToString());

        ShowErrorMessage(message);

    }

}

 
 

No comments:

Post a Comment