/// <summary> /// Gets Gmail request Token. /// </summary> /// <param name="callback">OAuth callback Url.</param> /// <exception cref="ArgumentNullException">Is raised when <b>callback</b> is null reference.</exception> /// <exception cref="InvalidOperationException">Is raised when this method is called in invalid state.</exception> public void GetRequestToken(string callback) { if (callback == null) { throw new ArgumentNullException("callback"); } if (!string.IsNullOrEmpty(m_RequestToken)) { throw new InvalidOperationException("Invalid state, you have already called this 'GetRequestToken' method."); } // For more info see: http://googlecodesamples.com/oauth_playground/ string timestamp = GenerateTimeStamp(); string nonce = GenerateNonce(); string url = "https://www.google.com/accounts/OAuthGetRequestToken?scope=" + UrlEncode(m_Scope); string sigUrl = "https://www.google.com/accounts/OAuthGetRequestToken"; // Build signature base. StringBuilder xxx = new StringBuilder(); xxx.Append("oauth_callback=" + UrlEncode(callback)); xxx.Append("&oauth_consumer_key=" + UrlEncode(m_ConsumerKey)); xxx.Append("&oauth_nonce=" + UrlEncode(nonce)); xxx.Append("&oauth_signature_method=" + UrlEncode("HMAC-SHA1")); xxx.Append("&oauth_timestamp=" + UrlEncode(timestamp)); xxx.Append("&oauth_version=" + UrlEncode("1.0")); xxx.Append("&scope=" + UrlEncode(m_Scope)); string signatureBase = "GET" + "&" + UrlEncode(sigUrl) + "&" + UrlEncode(xxx.ToString()); // Calculate signature. string signature = ComputeHmacSha1Signature(signatureBase, m_ConsumerSecret, null); //Build Authorization header. StringBuilder authHeader = new StringBuilder(); //authHeader.Append("Authorization: OAuth "); authHeader.Append("oauth_version=\"1.0\", "); authHeader.Append("oauth_nonce=\"" + nonce + "\", "); authHeader.Append("oauth_timestamp=\"" + timestamp + "\", "); authHeader.Append("oauth_consumer_key=\"" + m_ConsumerKey + "\", "); authHeader.Append("oauth_callback=\"" + UrlEncode(callback) + "\", "); authHeader.Append("oauth_signature_method=\"HMAC-SHA1\", "); authHeader.Append("oauth_signature=\"" + UrlEncode(signature) + "\""); // Create web request and read response. Helpers.SendHttpRequest(url, "GET", contentString: null, additionalHeaders: new Dictionary <String, String>() { { "Authorization", "OAuth " + authHeader.ToString() } }, handleResponseFunc: (WebResponse response) => { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { foreach (string parameter in HttpUtility.UrlDecode(reader.ReadToEnd()).Split('&')) { string[] name_value = parameter.Split('='); if (string.Equals(name_value[0], "oauth_token", Helpers.GetDefaultIgnoreCaseComparison())) { m_RequestToken = name_value[1]; } else if (string.Equals(name_value[0], "oauth_token_secret", Helpers.GetDefaultIgnoreCaseComparison())) { m_RequestTokenSecret = name_value[1]; } } } }); }
internal void FinishInitialization() { string host = UserHostName; if (_version > HttpVersion.Version10 && (host == null || host.Length == 0)) { _context.ErrorMessage = "Invalid host name"; return; } string path; Uri raw_uri = null; if (MaybeUri(_rawUrl.ToLowerInvariant()) && Uri.TryCreate(_rawUrl, UriKind.Absolute, out raw_uri)) { path = raw_uri.PathAndQuery; } else { path = _rawUrl; } if ((host == null || host.Length == 0)) { host = UserHostAddress; } if (raw_uri != null) { host = raw_uri.Host; } int colon = host.IndexOf(':'); if (colon >= 0) { host = host.Substring(0, colon); } string base_uri = string.Format("{0}://{1}:{2}", RequestScheme, host, LocalEndPoint.Port); if (!Uri.TryCreate(base_uri + path, UriKind.Absolute, out _requestUri)) { _context.ErrorMessage = WebUtility.HtmlEncode("Invalid url: " + base_uri + path); return; } _requestUri = HttpListenerRequestUriBuilder.GetRequestUri(_rawUrl, _requestUri.Scheme, _requestUri.Authority, _requestUri.LocalPath, _requestUri.Query); if (_version >= HttpVersion.Version11) { string t_encoding = Headers[HttpKnownHeaderNames.TransferEncoding]; _isChunked = (t_encoding != null && string.Equals(t_encoding, "chunked", StringComparison.OrdinalIgnoreCase)); // 'identity' is not valid! if (t_encoding != null && !_isChunked) { _context.Connection.SendError(null, 501); return; } } if (!_isChunked && !_clSet) { if (string.Equals(_method, "POST", StringComparison.OrdinalIgnoreCase) || string.Equals(_method, "PUT", StringComparison.OrdinalIgnoreCase)) { _context.Connection.SendError(null, 411); return; } } if (String.Compare(Headers[HttpKnownHeaderNames.Expect], "100-continue", StringComparison.OrdinalIgnoreCase) == 0) { HttpResponseStream output = _context.Connection.GetResponseStream(); output.InternalWrite(s_100continue, 0, s_100continue.Length); } }
/// <summary> /// Gets user Gmail email address. /// </summary> /// <returns>Returns user Gmail email address.</returns> /// <exception cref="InvalidOperationException">Is raised when this method is called in invalid state.</exception> public string GetUserEmail() { if (string.IsNullOrEmpty(m_AccessToken)) { throw new InvalidOperationException("Invalid state, you need to call 'GetAccessToken' method first."); } string url = "https://www.googleapis.com/userinfo/email"; string timestamp = GenerateTimeStamp(); string nonce = GenerateNonce(); // Build signature base. StringBuilder xxx = new StringBuilder(); xxx.Append("oauth_consumer_key=" + UrlEncode(m_ConsumerKey)); xxx.Append("&oauth_nonce=" + UrlEncode(nonce)); xxx.Append("&oauth_signature_method=" + UrlEncode("HMAC-SHA1")); xxx.Append("&oauth_timestamp=" + UrlEncode(timestamp)); xxx.Append("&oauth_token=" + UrlEncode(m_AccessToken)); xxx.Append("&oauth_version=" + UrlEncode("1.0")); string signatureBase = "GET" + "&" + UrlEncode(url) + "&" + UrlEncode(xxx.ToString()); // Calculate signature. string signature = ComputeHmacSha1Signature(signatureBase, m_ConsumerSecret, m_AccessTokenSecret); //Build Authorization header. StringBuilder authHeader = new StringBuilder(); //authHeader.Append("Authorization: OAuth "); authHeader.Append("oauth_version=\"1.0\", "); authHeader.Append("oauth_nonce=\"" + nonce + "\", "); authHeader.Append("oauth_timestamp=\"" + timestamp + "\", "); authHeader.Append("oauth_consumer_key=\"" + m_ConsumerKey + "\", "); authHeader.Append("oauth_token=\"" + UrlEncode(m_AccessToken) + "\", "); authHeader.Append("oauth_signature_method=\"HMAC-SHA1\", "); authHeader.Append("oauth_signature=\"" + UrlEncode(signature) + "\""); // Create web request and read response. Helpers.SendHttpRequest(url, "GET", contentString: null, additionalHeaders: new Dictionary <String, String>() { { "Authorization", "OAuth " + authHeader.ToString() } }, handleResponseFunc: (WebResponse response) => { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { foreach (string parameter in HttpUtility.UrlDecode(reader.ReadToEnd()).Split('&')) { string[] name_value = parameter.Split('='); if (string.Equals(name_value[0], "email", Helpers.GetDefaultIgnoreCaseComparison())) { m_Email = name_value[1]; } } } }); return(m_Email); }
HttpListener SearchListener(Uri uri, out ListenerPrefix prefix) { prefix = null; if (uri == null) { return(null); } string host = uri.Host; int port = uri.Port; string path = WebUtility.UrlDecode(uri.AbsolutePath); string path_slash = path [path.Length - 1] == '/' ? path : path + "/"; HttpListener best_match = null; int best_length = -1; if (host != null && host != "") { Hashtable p_ro = prefixes; foreach (ListenerPrefix p in p_ro.Keys) { string ppath = p.Path; if (ppath.Length < best_length) { continue; } if (p.Host != host || p.Port != port) { continue; } if (path.StartsWith(ppath) || path_slash.StartsWith(ppath)) { best_length = ppath.Length; best_match = (HttpListener)p_ro [p]; prefix = p; } } if (best_length != -1) { return(best_match); } } ArrayList list = unhandled; best_match = MatchFromList(host, path, list, out prefix); if (path != path_slash && best_match == null) { best_match = MatchFromList(host, path_slash, list, out prefix); } if (best_match != null) { return(best_match); } list = all; best_match = MatchFromList(host, path, list, out prefix); if (path != path_slash && best_match == null) { best_match = MatchFromList(host, path_slash, list, out prefix); } if (best_match != null) { return(best_match); } return(null); }