Ejemplo n.º 1
0
        private void beginButton_Click(object sender, RoutedEventArgs e)
        {
            try {
                var service = new ServiceProviderDescription {
                    RequestTokenEndpoint      = new MessageReceivingEndpoint(this.requestTokenUrlBox.Text, this.requestTokenHttpMethod.SelectedIndex == 0 ? HttpDeliveryMethods.GetRequest : HttpDeliveryMethods.PostRequest),
                    UserAuthorizationEndpoint = new MessageReceivingEndpoint(this.authorizeUrlBox.Text, HttpDeliveryMethods.GetRequest),
                    AccessTokenEndpoint       = new MessageReceivingEndpoint(this.accessTokenUrlBox.Text, this.accessTokenHttpMethod.SelectedIndex == 0 ? HttpDeliveryMethods.GetRequest : HttpDeliveryMethods.PostRequest),
                    TamperProtectionElements  = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
                    ProtocolVersion           = this.oauthVersion.SelectedIndex == 0 ? ProtocolVersion.V10 : ProtocolVersion.V10a,
                };
                var tokenManager = new InMemoryTokenManager();
                tokenManager.ConsumerKey    = this.consumerKeyBox.Text;
                tokenManager.ConsumerSecret = this.consumerSecretBox.Text;

                var    consumer = new DesktopConsumer(service, tokenManager);
                string accessToken;
                if (service.ProtocolVersion == ProtocolVersion.V10)
                {
                    string requestToken;
                    Uri    authorizeUrl = consumer.RequestUserAuthorization(null, null, out requestToken);
                    Process.Start(authorizeUrl.AbsoluteUri);
                    MessageBox.Show(this, "Click OK when you've authorized the app.");
                    var authorizationResponse = consumer.ProcessUserAuthorization(requestToken, null);
                    accessToken = authorizationResponse.AccessToken;
                }
                else
                {
                    var authorizePopup = new Authorize(
                        consumer,
                        (DesktopConsumer c, out string requestToken) => c.RequestUserAuthorization(null, null, out requestToken));
                    authorizePopup.Owner = this;
                    bool?result = authorizePopup.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        accessToken = authorizePopup.AccessToken;
                    }
                    else
                    {
                        return;
                    }
                }
                HttpDeliveryMethods resourceHttpMethod = this.resourceHttpMethodList.SelectedIndex < 2 ? HttpDeliveryMethods.GetRequest : HttpDeliveryMethods.PostRequest;
                if (this.resourceHttpMethodList.SelectedIndex == 1)
                {
                    resourceHttpMethod |= HttpDeliveryMethods.AuthorizationHeaderRequest;
                }
                var resourceEndpoint = new MessageReceivingEndpoint(this.resourceUrlBox.Text, resourceHttpMethod);
                using (IncomingWebResponse resourceResponse = consumer.PrepareAuthorizedRequestAndSend(resourceEndpoint, accessToken)) {
                    this.resultsBox.Text = resourceResponse.GetResponseReader().ReadToEnd();
                }
            } catch (DotNetOpenAuth.Messaging.ProtocolException ex) {
                MessageBox.Show(this, ex.Message);
            }
        }
Ejemplo n.º 2
0
        private void beginAuthorizationButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(this.googleTokenManager.ConsumerKey))
            {
                MessageBox.Show(this, "You must modify the App.config or OAuthConsumerWpf.exe.config file for this application to include your Google OAuth consumer key first.", "Configuration required", MessageBoxButton.OK, MessageBoxImage.Stop);
                return;
            }

            Authorize auth = new Authorize(
                this.google,
                (DesktopConsumer consumer, out string requestToken) =>
                GoogleConsumer.RequestAuthorization(
                    consumer,
                    GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger,
                    out requestToken));
            bool?result = auth.ShowDialog();

            if (result.HasValue && result.Value)
            {
                this.googleAccessToken    = auth.AccessToken;
                this.postButton.IsEnabled = true;

                XDocument contactsDocument = GoogleConsumer.GetContacts(this.google, this.googleAccessToken, 25, 1);
                var       contacts         = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom"))
                                             select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value, Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value };
                this.contactsGrid.Children.Clear();
                foreach (var contact in contacts)
                {
                    this.contactsGrid.RowDefinitions.Add(new RowDefinition());
                    TextBlock name = new TextBlock {
                        Text = contact.Name
                    };
                    TextBlock email = new TextBlock {
                        Text = contact.Email
                    };
                    Grid.SetRow(name, this.contactsGrid.RowDefinitions.Count - 1);
                    Grid.SetRow(email, this.contactsGrid.RowDefinitions.Count - 1);
                    Grid.SetColumn(email, 1);
                    this.contactsGrid.Children.Add(name);
                    this.contactsGrid.Children.Add(email);
                }
            }
        }
Ejemplo n.º 3
0
        private void beginWcfAuthorizationButton_Click(object sender, RoutedEventArgs e)
        {
            var requestArgs = new Dictionary <string, string>();

            requestArgs["scope"] = "http://tempuri.org/IDataApi/GetName|http://tempuri.org/IDataApi/GetAge|http://tempuri.org/IDataApi/GetFavoriteSites";
            Authorize auth = new Authorize(
                this.wcf,
                (DesktopConsumer consumer, out string requestToken) => consumer.RequestUserAuthorization(requestArgs, null, out requestToken));

            auth.Owner = this;
            bool?result = auth.ShowDialog();

            if (result.HasValue && result.Value)
            {
                this.wcfAccessToken           = auth.AccessToken;
                this.wcfName.Content          = CallService(client => client.GetName());
                this.wcfAge.Content           = CallService(client => client.GetAge());
                this.wcfFavoriteSites.Content = CallService(client => string.Join(", ", client.GetFavoriteSites()));
            }
        }
Ejemplo n.º 4
0
        private async void beginButton_Click(object sender, RoutedEventArgs e)
        {
            try {
                var service = new ServiceProviderDescription(
                    this.requestTokenUrlBox.Text,
                    this.authorizeUrlBox.Text,
                    this.accessTokenUrlBox.Text);

                var consumer = new Consumer(this.consumerKeyBox.Text, this.consumerSecretBox.Text, service, new MemoryTemporaryCredentialStorage());
                DotNetOpenAuth.OAuth.AccessToken accessToken;
                var authorizePopup = new Authorize(consumer, c => c.RequestUserAuthorizationAsync(null, null));
                authorizePopup.Owner = this;
                bool?result = authorizePopup.ShowDialog();
                if (result.HasValue && result.Value)
                {
                    accessToken = authorizePopup.AccessToken;
                }
                else
                {
                    return;
                }

                HttpMethod resourceHttpMethod = this.resourceHttpMethodList.SelectedIndex < 2 ? HttpMethod.Get : HttpMethod.Post;
                using (var handler = consumer.CreateMessageHandler(accessToken)) {
                    handler.Location = this.resourceHttpMethodList.SelectedIndex == 1
                                                                                   ? OAuth1HttpMessageHandlerBase.OAuthParametersLocation.AuthorizationHttpHeader
                                                                                   : OAuth1HttpMessageHandlerBase.OAuthParametersLocation.QueryString;
                    using (var httpClient = consumer.CreateHttpClient(handler)) {
                        var request = new HttpRequestMessage(resourceHttpMethod, this.resourceUrlBox.Text);
                        using (var resourceResponse = await httpClient.SendAsync(request)) {
                            this.resultsBox.Text = await resourceResponse.Content.ReadAsStringAsync();
                        }
                    }
                }
            } catch (DotNetOpenAuth.Messaging.ProtocolException ex) {
                MessageBox.Show(this, ex.Message);
            }
        }