Beispiel #1
0
        /// <summary>
        ///   Authenticate to the named Google/GData service, eg picasa,
        ///   with the given user name.
        /// </summary>
        /// <remarks>
        ///   <para>
        ///     If the provided username is blank, then this method will
        ///     prompt the user to provide it, by displaying a form.  It
        ///     then will send an authentication message to Google.
        ///     Upon receipt of a successful authentication message, the
        ///     Google authentication token will be stored into this
        ///     (singleton) instance.
        ///   </para>
        /// </remarks>
        /// <returns>
        //    The user name (email addy) which is authenticated.
        /// </returns>
        public static string Authenticate(string username, string service)
        {
            Tracing.Trace("GDataSession::Authenticate user({0}), svc({1})",
                          username, service);
            var serviceString = LookupService(service);

            if (GdataSession.GetHeaders(username, service) != null)
            {
                return(username);
            }
            var info = PromptForAuthInfo(username);

            if (info == null)
            {
                return(null);
            }

            var http = new HttpClient(_baseLoginUrl);
            var form = new HttpUrlEncodedForm();

            form.Add("accountType", "GOOGLE");
            form.Add("Email", info.email);
            form.Add("Passwd", info.password);
            form.Add("service", serviceString);
            form.Add("source", _appName);

            var response = http.Post(_relativeLoginUrl, form.CreateHttpContent());

            response.EnsureStatusIsSuccessful();

            var    result = response.Content.ReadAsString();
            string auth   = RE.Regex.Replace(result, "(?s).*Auth=(.*)", "$1");

            // After a successful authentication request, use the Auth
            // value to create an Authorization header for each request:
            //
            // Authorization: GoogleLogin auth=yourAuthValue
            //
            // Hereafter, we need to use a HttpClient.Send() overload, in
            // order to be able to specify headers.

            instance.SetAuth(info.email, service, auth);
            Tracing.Trace("GDataSession::Authenticate return '{0}'",
                          info.email);

            return(info.email);
        }
Beispiel #2
0
        /// <summary>
        ///  This does the real work of the plugin - uploading to Picasa.
        /// </summary>
        ///
        /// <remarks>
        ///   First upload the main image, and then place the raw
        ///   image URL onto the clipboard for easy reference/paste.
        /// </remarks>
        private void UploadImage()
        {
            Tracing.Trace("Picasa::{0:X8}::UploadImage", this.GetHashCode());

            try
            {
                var address =
                    GdataSession.Authenticate(PluginSettings.EmailAddress, "picasa");

                // reset this in case user has changed it in the authn dialog.
                PluginSettings.EmailAddress = address;

                var headers =
                    GdataSession.GetHeaders(PluginSettings.EmailAddress, "picasa");

                // user has declined or failed to authenticate
                if (headers == null)
                {
                    return;
                }

                // Now, upload the photo...
                var summaryText = GetSummaryText();

                var uploadform = new PicasaUploadHttpForm
                {
                    File    = this._fileName,
                    Summary = summaryText
                };

                var u = String.Format("/data/feed/api/user/default/albumid/{0}",
                                      PluginSettings.Album.Id);

                Tracing.Trace("Upload to album: {0} ({1})",
                              PluginSettings.Album.Name,
                              PluginSettings.Album.Id);

                var uri = new Uri(u, UriKind.RelativeOrAbsolute);

                // POST the request
                using (var requestMsg =
                           new HttpRequestMessage("POST",
                                                  uri,
                                                  headers,
                                                  uploadform.CreateHttpContent()))
                {
                    using (var picasa = new HttpClient(_basePicasaUrl))
                    {
                        var response = picasa.Send(requestMsg);
                        response.EnsureStatusIsSuccessful();
                        var    foo         = response.Content.ReadAsXmlSerializable <UploadResponse>();
                        string rawImageUri = foo.content.mediaUrl;
                        if (PluginSettings.PopBrowser)
                        {
                            System.Diagnostics.Process.Start(rawImageUri);
                        }
                        Clipboard.SetDataObject(rawImageUri, true);
                    }
                }

                Tracing.Trace("all done.");
                Tracing.Trace("---------------------------------");
            }
            catch (Exception exc1)
            {
                Tracing.Trace("Exception. {0}", exc1.Message);
                Tracing.Trace("{0}", exc1.StackTrace);

                Tracing.Trace("---------------------------------");
                MessageBox.Show("There's been an exception uploading your image:" +
                                Environment.NewLine +
                                exc1.Message +
                                Environment.NewLine +
                                Environment.NewLine +
                                "You will have to upload this file manually: " +
                                Environment.NewLine +
                                this._fileName,
                                "Cropper Plugin for Picasa",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            return;
        }
        private void btnRefreshAlbumList_Click(object sender, System.EventArgs e)
        {
            var c = this.Cursor;

            this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            string username = null;

            try
            {
                username =
                    GdataSession.Authenticate(txtEmailAddress.Text, "picasa");
            }
            finally
            {
                this.Cursor = c;
            }

            if (String.IsNullOrEmpty(username))
            {
                return;
            }

            txtEmailAddress.Text = username;

            cmbAlbum.Items.Clear();

            // To get a list of albums:
            // GET https://picasaweb.google.com/data/feed/api/user/userID

            var picasa = new HttpClient("https://picasaweb.google.com/");

            var headers =
                GdataSession.GetHeaders(username, "picasa");

            // get a list of albums
            var response = picasa.Send(HttpMethod.GET,
                                       "/data/feed/api/user/default",
                                       headers);

            response.EnsureStatusIsSuccessful();
            Tracing.Trace("PicasaOptionsForm: Response is success");

            var feed = response.Content.ReadAsSyndicationFeed();

            Tracing.Trace("PicasaOptionsForm: found {0} items", feed.Items.Count());

            var selection = from i in feed.Items
                            where i.Title.Text == "Drop Box"
                            select i;

            bool foundDropBox = (selection.Count() == 1);

            if (!foundDropBox)
            {
                Tracing.Trace("PicasaOptionsForm: no drop box - adding it");
                cmbAlbum.Items.Add(new AlbumItem
                {
                    Name = "Drop Box",
                    Id   = "default"
                });
            }

            foreach (var item in feed.Items)
            {
                if (foundDropBox || item.Title.Text != "Drop Box")
                {
                    Tracing.Trace("PicasaOptionsForm: album({0}) id({1})",
                                  item.Title.Text,
                                  RE.Regex.Replace(item.Id, ".*/([^/]+)", "$1"));

                    cmbAlbum.Items.Add(new AlbumItem
                    {
                        Name = item.Title.Text,
                        Id   = RE.Regex.Replace(item.Id, ".*/([^/]+)", "$1")
                    });
                }
            }
        }