3 messages in com.googlegroups.google-picasa-data-apiRe: list private albums in a asp.net ...
FromSent OnAttachments
johbo05 Jan 2008 00:13 
Ryan Boyd (Google)05 Jan 2008 15:48 
johbo10 Jan 2008 10:10 
Subject:Re: list private albums in a asp.net webservice
From:johbo (jj.b@gmail.com)
Date:01/10/2008 10:10:09 AM
List:com.googlegroups.google-picasa-data-api

Hello Ryan,

Thanks for putting me on the right trail, i'vo got it working. I have posted an ASP.NET example in this message, perhaps other visitors of this forum can also use it.

A working demo is at: http://www.familieboers.eu/wt/picasa_api/

kind greeting,

John

===== ASP.NET sample code =====

<%@ Page Language="VB" trace="true" debug="true"%> <%@ Import Namespace="System.Net"%> <%@ Import Namespace="System.IO"%> <%@ Import Namespace="System.data"%> <%@ Import Namespace="System.xml"%> <script language="VB" runat="server"> ' ----- ' Picasa Web Album demo ' ----- ' shows the maps in your picasa account ' If your login succeeds even private albums become visible ' ' John boers (http://www.familieboers.eu/) ' 2008-01-10 ' ----- '----- put your own info here (or even better in your web.config ----- const strEmail="[Your Email]" 'Email of your Google account const strPassw="[Your Password]" 'Your password const strAccount="[Your Account]" 'Your account name const strApp="[Your Applications name]" 'Can be anything is used by Google for logging purposes

Sub Page_Load(sender as Object, e as EventArgs) ' first get your authorization Dim strAuth = GetAuth(strEmail,strPassw,strApp) ' if you have got you're Auth string get the albums list if strAuth<> "n/a" then GetAlbums(strAuth) end if End Sub

function GetAuth(email as string, password as string, appId as string) as string ' Do a webrequest to Google and give you're credentials in the ' Post information. Dim myRequest as HttpWebRequest = myRequest.Create("https:// www.google.com/accounts/ClientLogin") myRequest.ContentType = "application/x-www-form-urlencoded" myRequest.Method = "POST" ' buiold the post string Dim myPost as stringbuilder=new stringbuilder myPost.AppendFormat("Email={0}", email) myPost.AppendFormat("&Passwd={0}", password) myPost.AppendFormat("&service={0}", "lh2") myPost.AppendFormat("&source={0}", appId) trace.write(myPost.ToString()) ' write the post-string as a byte array Dim myData as byte() = ASCIIEncoding.ASCII.GetBytes(myPost.ToString()) myRequest.ContentLength = myData.Length Dim myStream as stream = myRequest.GetRequestStream() myStream.Write(myData, 0, myData.Length) myStream.Close() ' Do the actual request and read the response stream Dim myResponse as HttpWebResponse = myRequest.GetResponse() Dim myResponseStream as stream = myResponse.GetResponseStream() Dim myResponseReader as streamreader = new StreamReader(myResponseStream) Dim strResponse as string = myResponseReader.ReadToEnd() myResponseReader.Close() myResponseStream.Close() trace.write(strResponse) ' if you can find a Auth= part in the response then read it. Dim strAuth as string if strResponse.LastIndexOf("Auth=")>0 then Dim i as integer = strResponse.LastIndexOf("=") + 1 strAuth = strResponse.Substring(i, strResponse.Length - i) trace.write("Auth=",strAuth) ' HaalDocument(strAuth) else ' no Auth= found strAuth = "n/a" end if 'give the value back return strAuth end function

sub GetAlbums(strAuth as string) Dim result as string ' ask for a webresponse from picasa Dim objResponse as httpWebResponse Dim objRequest as WebRequest = HttpWebRequest.Create("http:// picasaweb.google.com/data/feed/api/user/" & strAccount & "? kind=album") ' by default you get only the public albums ' add your authorization code and you will also get your private albums objRequest.Headers.Add("Authorization: GoogleLogin auth=" & strAuth) objRequest.Method = "GET" try ' do the request objResponse = objRequest.GetResponse() catch exc as exception ' whn the authorization code is incorrect the api gives a status ' 500: internal server error response.write(exc.message) response.end end try ' when te reponse gives a statuscode 200 then eveything went ok if objResponse.StatusCode = 200 then ' now read the response stream Dim sr as StreamReader = new StreamReader(objResponse.GetResponseStream()) result = sr.ReadToEnd() sr.Close() ' prepare a datatable to hold and display the information Dim tblAlbums as DataTable = new DataTable("albums") Dim dcId As DataColumn = New DataColumn("id") dcId.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcId) Dim dcTitle As DataColumn = New DataColumn("title") dcTitle.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcTitle) Dim dcSum As DataColumn = New DataColumn("summary") dcSum.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcSum) Dim dcAcc As DataColumn = New DataColumn("access") dcAcc.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcAcc) Dim dcThumb As DataColumn = New DataColumn("thumb") dcThumb.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcThumb) Dim dcCoords As DataColumn = New DataColumn("Coords") dcCoords.DataType = System.Type.GetType("System.String") tblAlbums.Columns.Add(dcCoords) ' I now have the XML-response in a string, now I can start processing the XML ' define a xml-document and feed the Google response to it Dim doc as XmlDocument = New XmlDocument() doc.LoadXML(result) ' define the namespaces used in the document Dim nsMgr as XmlNamespaceManager = new XmlNamespaceManager(Doc.NameTable) nsMgr.AddNamespace("gphoto", "http://schemas.google.com/photos/ 2007") nsMgr.AddNamespace("batch", "http://schemas.google.com/gdata/ batch") nsMgr.AddNamespace("photo", "http://www.pheed.com/pheed/") nsMgr.AddNamespace("media", "http://search.yahoo.com/mrss/") nsMgr.AddNamespace("georss", "http://www.georss.org/georss") nsMgr.AddNamespace("photo", "http://www.pheed.com/pheed/") ' I want info about my albums, all this info is ' between the <entry> and </entry> tags so get a list of those nodes Dim lstAlbums as XmlNodeList = doc.GetElementsByTagName( "entry" ) ' 'How many albums are found? response.write(lstAlbums.count & " albums<br>") ' Next loop through the nodes and get the information I want to see. Dim ndAlbum as XmlNode For each ndAlbum in lstAlbums ' must have a new row in the datatable Dim NewRow as DataRow = tblAlbums.NewRow() for Each ndAlbumProp as XmlNode in ndAlbum.ChildNodes trace.write(ndAlbumProp.Name) select case ndAlbumProp.Name case "id" NewRow("id")=ndAlbumProp.InnerText case "title" NewRow("title")=ndAlbumProp.InnerText case "summary" NewRow("summary")=ndAlbumProp.InnerText case "gphoto:access" NewRow("access")=ndAlbumProp.InnerText case "media:group" ' the info here is in childnodes so let's get them. for Each ndMediaProp as xmlnode in ndAlbumProp.ChildNodes select case ndMediaProp.Name case "media:thumbnail" ' to get a attribut I need the node as element Dim elNode as XmlElement = ndMediaProp NewRow("thumb")=elNode.GetAttribute("url") end select next case "georss:where" ' in fact the info is in the chilnodes: ' gml:point and gml:pos ' but the coorinates are the only text so this will do NewRow("coords")=ndAlbumProp.InnerText end select

next ' now add the info to the table tblAlbums.Rows.Add(NewRow) next response.write(tblAlbums.rows.count & "<hr>") ' Display the information in a datagrid MyAlbums.DataSource=tblAlbums MyAlbums.DataBind() else result= "fout, statuscode: " & objResponse.StatusCode end if trace.write(result) end sub </script>

<html> <body> <h1>Picasa WebAlbum API demo</h1> <asp:DataGrid runat="server" id="MyAlbums" AutoGenerateColumns="false" CellPadding="4" style="width:100%;" border="0" HorizontalAlign="Center" > <Columns> <asp:templatecolumn headertext="thumb"> <itemtemplate> <asp:image imageurl='<%#databinder.eval(container.dataitem, "thumb") %>' runat="server"> </asp:image> </itemtemplate> </asp:templatecolumn> <asp:TemplateColumn Headertext="album"> <ItemTemplate> <b><%# DataBinder.Eval(Container.DataItem, "title") %></b> <br> access: <font style="color:red;"><%# DataBinder.Eval(Container.DataItem, "access") %></font> <br> id: <%# DataBinder.Eval(Container.DataItem, "id") %> <br> summary: <%# DataBinder.Eval(Container.DataItem, "summary") %> <br> coordinates: <%# DataBinder.Eval(Container.DataItem, "coords") %> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:datagrid> </body>