Esempio n. 1
0
        Task GetAccessTokenAsync()
        {
            #if DEBUG
            StringBuilder sb = new StringBuilder();
            sb.AppendLine($"OAuth1Authenticator.GetAccessTokenAsync ");
            sb.AppendLine($"        token = {token}");
            System.Diagnostics.Debug.WriteLine(sb.ToString());
            #endif

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

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

            // WebRequest Replaced with HttpRequest for .net Standard 1.1
            HttpWebRequest req = OAuth1.CreateRequest
                                 (
                "GET",
                accessTokenUrl,
                request_parameters,
                consumerKey,
                consumerSecret,
                tokenSecret
                                 );

            if (this.HttpWebClientFrameworkType == HttpWebClientFrameworkType.WebRequest)
            {
                WebResponse response = req.GetResponseAsync().Result;

                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);
        }
Esempio n. 2
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(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_original;

            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);
        }