Пример #1
0
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync()
        {
            var req = OAuth1.CreateRequest(
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", callbackUrl.AbsoluteUri },
            },
                consumerKey,
                consumerSecret,
                "");

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var r = WebEx.FormDecode(content);

                token = r["oauth_token"];
                tokenSecret = r["oauth_token_secret"];

                var url = authorizeUrl.AbsoluteUri + "?oauth_token=" + Uri.EscapeDataString(token);

                return new Uri(url);
            }));
        }
        public override Task <Uri> GetInitialUrlAsync()
        {
            var req = OAuth1.CreateRequest(
                "GET",
                requestTokenUrl,
                new Dictionary <string, string> (),
                consumerKey,
                consumerSecret,
                ""
                );

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var r = WebEx.FormDecode(content);

                token = r["oauth_token"];
                tokenSecret = r["oauth_token_secret"];

                string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                var url = String.Format("{0}{1}oauth_token={2}&oauth_callback={3}",
                                        authorizeUrl.AbsoluteUri,
                                        paramType,
                                        Uri.EscapeDataString(token),
                                        Uri.EscapeDataString(callbackUrl.AbsoluteUri));

                return new Uri(url);
            }));
        }
Пример #3
0
        /// <summary>
        /// Gets OAuth authorization header.
        /// </summary>
        protected virtual string GetAuthorizationHeader()
        {
            //
            // Make sure that the parameters array contains
            // mulitpart keys if we're dealing with a buggy
            // OAuth implementation (I'm looking at you Flickr).
            //
            // These normally shouldn't be included: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
            //
            var ps = new Dictionary <string, string> (Parameters);

            if (includeMultipartsInSignature)
            {
                foreach (var p in Multiparts)
                {
                    if (!string.IsNullOrEmpty(p.TextData))
                    {
                        ps [p.Name] = p.TextData;
                    }
                }
            }

            return(OAuth1.GetAuthorizationHeader(
                       Method,
                       Url,
                       ps,
                       Account.Properties ["oauth_consumer_key"],
                       Account.Properties ["oauth_consumer_secret"],
                       Account.Properties ["oauth_token"],
                       Account.Properties ["oauth_token_secret"]));
        }
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync()
        {
            /*
             *                  mc++
             *                  OriginalString property of the Uri object should be used instead of AbsoluteUri
             *                  otherwise trailing slash is added.
             */
            string oauth_callback_uri_absolute = callbackUrl.AbsoluteUri;
            string oauth_callback_uri_original = callbackUrl.OriginalString;

            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.AbsoluteUri    = " + oauth_callback_uri_absolute);
            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.OriginalString = " + oauth_callback_uri_original);

            string oauth_callback_uri = oauth_callback_uri_absolute;

            var req = OAuth1.CreateRequest
                      (
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", oauth_callback_uri },
            },
                consumerKey,
                consumerSecret,
                ""
                      );

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync()
                       .ContinueWith
                       (
                           respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var r = WebEx.FormDecode(content);

                    token = r["oauth_token"];
                    tokenSecret = r["oauth_token_secret"];

                    string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                    var url = authorizeUrl.AbsoluteUri + paramType + "oauth_token=" + Uri.EscapeDataString(token);
                    return new Uri(url);
                }
                       ));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
Пример #5
0
        Task GetAccessTokenAsync()
        {
            var requestParams = new Dictionary <string, string> {
                { "oauth_token", token }
            };

            if (verifier != null)
            {
                requestParams["oauth_verifier"] = verifier;
            }

            var req = OAuth1.CreateRequest(
                "GET",
                accessTokenUrl,
                requestParams,
                consumerKey,
                consumerSecret,
                tokenSecret);

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync().ContinueWith(respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var accountProperties = WebEx.FormDecode(content);
                    accountProperties["oauth_consumer_key"] = consumerKey;
                    accountProperties["oauth_consumer_secret"] = consumerSecret;

                    if (getUsernameAsync != null)
                    {
                        getUsernameAsync(accountProperties).ContinueWith(uTask =>
                        {
                            if (uTask.IsFaulted)
                            {
                                OnError(uTask.Exception);
                            }
                            else
                            {
                                OnSucceeded(uTask.Result, accountProperties);
                            }
                        });
                    }
                    else
                    {
                        OnSucceeded("", accountProperties);
                    }
                }));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
Пример #6
0
        /// <summary>
        /// Gets the response.
        /// </summary>
        /// <returns>
        /// The response.
        /// </returns>
        public override Task <Response> GetResponseAsync(CancellationToken cancellationToken)
        {
            //
            // Make sure we have an account
            //
            if (Account == null)
            {
                throw new InvalidOperationException("You must specify an Account for this request to proceed");
            }

            //
            // Sign the request before getting the response
            //
            var req = GetPreparedWebRequest();

            //
            // Make sure that the parameters array contains
            // mulitpart keys if we're dealing with a buggy
            // OAuth implementation (I'm looking at you Flickr).
            //
            // These normally shouldn't be included: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
            //
            var ps = new Dictionary <string, string> (Parameters);

            if (includeMultipartsInSignature)
            {
                foreach (var p in Multiparts)
                {
                    if (!string.IsNullOrEmpty(p.TextData))
                    {
                        ps [p.Name] = p.TextData;
                    }
                }
            }

            //
            // Authorize it
            //
            var authorization = OAuth1.GetAuthorizationHeader(
                Method,
                Url,
                ps,
                Account.Properties ["oauth_consumer_key"],
                Account.Properties ["oauth_consumer_secret"],
                Account.Properties ["oauth_token"],
                Account.Properties ["oauth_token_secret"]);

            req.Headers [HttpRequestHeader.Authorization] = authorization;

            return(base.GetResponseAsync(cancellationToken));
        }
Пример #7
0
        Task GetAccessTokenAsync()
        {
            var requestparams = new Dictionary <string, string> {
                { "oauth_token", token },
            };

            if (verifier != null)
            {
                requestparams["oauth_verifier"] = verifier;
            }

            var req = OAuth1.CreateRequest(
                "GET",
                accessTokenUrl,
                requestparams,
                consumerKey,
                consumerSecret,
                tokenSecret);

            return(req.GetResponseAsync().ContinueWith(respTask => {
                var content = respTask.Result.GetResponseText();

                var accountProperties = WebEx.FormDecode(content);
                accountProperties["oauth_consumer_key"] = consumerKey;
                accountProperties["oauth_consumer_secret"] = consumerSecret;

                if (getUsernameAsync != null)
                {
                    getUsernameAsync(accountProperties).ContinueWith(uTask => {
                        if (uTask.IsFaulted)
                        {
                            OnError(uTask.Exception);
                        }
                        else
                        {
                            OnSucceeded(uTask.Result, accountProperties);
                        }
                    });
                }
                else
                {
                    OnSucceeded("", accountProperties);
                }
            }));
        }
Пример #8
0
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync()
        {
            var req = OAuth1.CreateRequest(
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", callbackUrl.AbsoluteUri },
            },
                consumerKey,
                consumerSecret,
                "");

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync().ContinueWith(respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var r = WebEx.FormDecode(content);

                    token = r["oauth_token"];
                    tokenSecret = r["oauth_token_secret"];

                    string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                    var url = authorizeUrl.AbsoluteUri + paramType + "oauth_token=" + Uri.EscapeDataString(token);
                    return new Uri(url);
                }));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
Пример #9
0
        /// <summary>
        /// Gets OAuth authorization header.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Make sure that the parameters array contains mulitpart keys if we're dealing with a buggy
        /// OAuth implementation (such as Flickr).
        /// </para>
        /// <para>
        /// These normally shouldn't be included: http://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
        /// </para>
        /// </remarks>
        protected virtual string GetAuthorizationHeader()
        {
            var ps = new Dictionary <string, string>(Parameters);

            if (includeMultipartsInSignature)
            {
                foreach (var p in Multiparts)
                {
                    if (!string.IsNullOrEmpty(p.TextData))
                    {
                        ps[p.Name] = p.TextData;
                    }
                }
            }

            return(OAuth1.GetAuthorizationHeader(
                       Method,
                       Url,
                       ps,
                       Account.Properties["oauth_consumer_key"],
                       Account.Properties["oauth_consumer_secret"],
                       Account.Properties["oauth_token"],
                       Account.Properties["oauth_token_secret"]));
        }
        Task GetAccessTokenAsync()
        {
            #if DEBUG
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"OAuth1Authenticator.GetAccessTokenAsync ");
            sb.AppendLine($"        token = {token}");
            System.Diagnostics.Debug.WriteLine(sb.ToString());
            #endif

            var requestParams = new Dictionary <string, string>
            {
                { "oauth_token", token }
            };

            if (verifier != null)
            {
                requestParams["oauth_verifier"] = verifier;
                System.Diagnostics.Debug.WriteLine($"        verifier = {verifier}");
            }

            var req = OAuth1.CreateRequest(
                "GET",
                accessTokenUrl,
                requestParams,
                consumerKey,
                consumerSecret,
                tokenSecret);

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync().ContinueWith(respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var accountProperties = WebEx.FormDecode(content);

                    accountProperties["oauth_consumer_key"] = consumerKey;
                    accountProperties["oauth_consumer_secret"] = consumerSecret;

                    if (getUsernameAsync != null)
                    {
                        getUsernameAsync(accountProperties).ContinueWith(uTask =>
                        {
                            if (uTask.IsFaulted)
                            {
                                OnError(uTask.Exception);
                            }
                            else
                            {
                                OnSucceeded(uTask.Result, accountProperties);
                            }
                        });
                    }
                    else
                    {
                        OnSucceeded("", accountProperties);
                    }
                }));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }
        /// <summary>
        /// Method that returns the initial URL to be displayed in the web browser.
        /// </summary>
        /// <returns>
        /// A task that will return the initial URL.
        /// </returns>
        public override Task <Uri> GetInitialUrlAsync(Dictionary <string, string> query_parameters = null)
        {
            /*
             *  mc++
             *  OriginalString property of the Uri object should be used instead of AbsoluteUri
             *
             *  otherwise trailing slash is added.
             *
             *  string[] uris = new string[]
             *  {
             *      "http://xamarin.com/",
             *      "http://xamarin.com",
             *  };
             *  foreach (string u in uris)
             *  {
             *      uri = new Uri(u);
             *      Console.WriteLine("uri.AbsoluteUri = " + uri.AbsoluteUri);
             *      Console.WriteLine("uri.OriginalString = " + uri.OriginalString);
             *  }
             *
             *  The problem is whether to send original string to be compared with registered
             *  redirect_url on the authorization server od "correct" url (AblsoluteUrl) with
             *  slash
             */
            string oauth_callback_uri_absolute = callbackUrl.AbsoluteUri;
            string oauth_callback_uri_original = callbackUrl.OriginalString;

            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.AbsoluteUri    = " + oauth_callback_uri_absolute);
            System.Diagnostics.Debug.WriteLine("GetInitialUrlAsync callbackUrl.OriginalString = " + oauth_callback_uri_original);

            string oauth_callback_uri = oauth_callback_uri_absolute;

            var req = OAuth1.CreateRequest
                      (
                "GET",
                requestTokenUrl,
                new Dictionary <string, string>()
            {
                { "oauth_callback", oauth_callback_uri },
            },
                consumerKey,
                consumerSecret,
                ""
                      );

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                return(req.GetResponseAsync()
                       .ContinueWith
                       (
                           respTask =>
                {
                    var content = respTask.Result.GetResponseText();

                    var r = WebEx.FormDecode(content);

                    token = r["oauth_token"];
                    tokenSecret = r["oauth_token_secret"];

                    string paramType = authorizeUrl.AbsoluteUri.IndexOf("?") >= 0 ? "&" : "?";

                    var url = authorizeUrl.AbsoluteUri + paramType + "oauth_token=" + Uri.EscapeDataString(token);
                    return new Uri(url);
                }
                       ));
            }
            else if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.HttpClient)
            {
                throw new NotImplementedException("HttpClient implementation!");
            }

            return(null);
        }