/// <summary>
        /// Update a photo metadata by atom entry.
        /// </summary>
        /// <param name="photoEntry">The entry containing the modified information</param>
        /// <param name="access_token">user access token</param>
        /// <returns>true on success else false</returns>
        public bool UpdatePhotoMetadataByEntry(SyndicationItem photoEntry, string access_token)
        {
            // PUT https://picasaweb.google.com/data/entry/api/user/default/albumid/albumID/photoid/photoID
            string photoID = string.Empty;
            string albumID = string.Empty;

            //get the photoid and albumid from the photoentry'
            SyndicationElementExtension ext = null;
            ext = photoEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("id")).First();
            photoID = ext.GetReader().ReadInnerXml();
            ext = photoEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("albumid")).First();
            albumID = ext.GetReader().ReadInnerXml();

            string updateURI = string.Format("{0}{1}{2}"
                                                , GoogleUtilityConstants.PICASA_ENTRY_BASE_URI_NO_HTTPS
                                                , GetAlbumURI(albumID)
                                                , GetPhotoURI(photoID));

            var output = new StringBuilder();
            var formatter = new Atom10ItemFormatter(photoEntry);

            using (var writer = XmlWriter.Create(output))
            {
                formatter.WriteTo(writer);
            }

            //var contentAsBytes = Encoding.ASCII.GetBytes(output.ToString());

            GoogleTCPRequestor tcpr = new GoogleTCPRequestor(Requestor.Operation.PUT
                                                            , Requestor.HTTPFormat.FORMAT_1_1
                                                            , Requestor.ContentType.ATOM_XML
                                                            , GoogleUtilityConstants.GOOGLE_PICASA_HOST
                                                            , updateURI
                                                            , output.ToString()
                                                            , access_token
                                                            , true);

            //submit the request:
            return tcpr.SubmitRequest(true, false, Requestor.ResponseTerminatorAction.CONTAINS_HTTP_1_1_200_OK);
        }
        /// <summary>
        /// Update the album by entry
        /// </summary>
        /// <param name="albumEntry"></param>
        /// <param name="access_token"></param>
        /// <returns></returns>
        public bool UpdateAlbum(SyndicationItem albumEntry, string access_token)
        {
            //get the album id
            string albumID = string.Empty;
            SyndicationElementExtension ext = albumEntry.ElementExtensions.Where(x => x.OuterName.ToLower().Equals("id")).First();
            albumID = ext.GetReader().ReadInnerXml();

            //create the URI
            string updateURI = string.Format("{0}{1}"
                                            , GoogleUtilityConstants.PICASA_ENTRY_BASE_URI_NO_HTTPS
                                            , string.Format(GoogleUtilityConstants.PICASA_ALBUMID_URI, albumID));
            //create the content body string
            var output = new StringBuilder();
            var formatter = new Atom10ItemFormatter(albumEntry);
            using (var writer = XmlWriter.Create(output))
            {
                formatter.WriteTo(writer);
            }

            //create the requestor
            GoogleTCPRequestor tcpr = new GoogleTCPRequestor(Requestor.Operation.PUT
                                                            , Requestor.HTTPFormat.FORMAT_1_1
                                                            , Requestor.ContentType.ATOM_XML
                                                            , GoogleUtilityConstants.GOOGLE_PICASA_HOST
                                                            , updateURI
                                                            , output.ToString()
                                                            , access_token
                                                            , true);

            //get the output
            string response = tcpr.SubmitRequest(true, Requestor.ResponseTerminatorAction.READ_TO_END);

            return response.Contains("HTTP/1.1 200 OK");
        }
        private void WriteAtomEntry(IPublication publication, Stream writeStream)
        {
            var entry = publication.Syndicate();

            var formatter = new Atom10ItemFormatter(entry);

            using (var writer = XmlWriter.Create(writeStream))
            {
                formatter.WriteTo(writer);
            }
        }
        private void WriteAtomMediaEntry(IPublicationMedia publication, Stream writeStream)
        {
            SyndicationItem mediaEntry = publication.Syndicate();

            Atom10ItemFormatter formatter = new Atom10ItemFormatter(mediaEntry);

            using (XmlWriter writer = XmlWriter.Create(writeStream))
            {
                formatter.WriteTo(writer);
            }
        }
        protected static HttpStatusCode SendContact(
            ConstantContactCredential credential, Uri uri, String method, Contact contact)
        {
            var request = WebRequest.Create(uri) as HttpWebRequest;

            if (request == null)
            {
                throw new WebException("Failed to create WebRequest");
            }

            request.Credentials = credential;
            request.Headers.Add("Authorization", String.Format("Basic {0}", 
                Convert.ToBase64String(Encoding.ASCII.GetBytes(credential.Password))));
            request.Method = method;
            request.ContentType = "application/atom+xml"; // "application/x-www-form-urlencoded";

            var contactItem = new SyndicationItem
                                  {
                                      Id = (contact.Id != null) ? contact.Id.ToString() : "data:,none",
                                      Content = new ContactContent(contact)
                                  };

            contactItem.Authors.Add(new SyndicationPerson(typeof(ContactsCollection).FullName));
            contactItem.Summary = new TextSyndicationContent("Contact");

            var atomFormatter = new Atom10ItemFormatter(contactItem);

            using (var memoryStream = new MemoryStream())
            {
                var writerSettings = new XmlWriterSettings
                {
                    Indent = true,
                    IndentChars = " ",
                    OmitXmlDeclaration = true,
                    Encoding = new UTF8Encoding(false),
                };

                var xmlWriter = XmlWriter.Create(memoryStream, writerSettings);

                if (xmlWriter == null)
                    throw new XmlException("Failed to create XmlWriter");

                atomFormatter.WriteTo(xmlWriter);
                xmlWriter.Flush();

                memoryStream.Seek(0, SeekOrigin.Begin);
                byte[] data = memoryStream.ToArray();
                memoryStream.Close();

                Debug.WriteLine(Encoding.UTF8.GetString(data));

                // Set the content length in the request headers  
                request.ContentLength = data.Length;

                // Write data  
                using (var postStream = request.GetRequestStream())
                {
                    if (data.Length > int.MaxValue)
                    {
                        throw new InvalidDataException(
                            String.Format("Contact content length exceeds {0} bytes", int.MaxValue));
                    }

                    postStream.Write(data, 0, data.Length);
                }
            }

            // Get response
            using (var webResponse = request.GetResponse() as HttpWebResponse)
            {
                if (webResponse == null)
                {
                    throw new WebException("Failed to get HttpWebResponse");
                }

                // Get the response stream  
                using (var reader = new StreamReader(webResponse.GetResponseStream()))
                {
                    var response = reader.ReadToEnd();
                    Debug.WriteLine(response);
                }

                return webResponse.StatusCode;
            }
        }
 public override void OnWriteToStream(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext context)
 {
     if (type.Equals(typeof(SyndicationItem)))
     {
         using (var writer = XmlWriter.Create(stream, WriterSettings))
         {
             var itemFormatter = new Atom10ItemFormatter((SyndicationItem)value);
             itemFormatter.WriteTo(writer);
             writer.Flush();
         }
     }
     else if (type.Equals(typeof(SyndicationFeed)))
     {
         using (var writer = XmlWriter.Create(stream, WriterSettings))
         {
             var feedFormatter = new Atom10FeedFormatter((SyndicationFeed)value);
             feedFormatter.WriteTo(writer);
             writer.Flush();
         }
     }
     else
     {
         throw new InvalidOperationException("Expected to be called with type SyndicationItem or SyndicationFeed.");
     }
 }