Example #1
0
        public static string GenerateHeader(Uri uri, String consumerKey, String consumerSecret, String token, String tokenSecret, String httpMethod)
        {
            OAuthUtil oauthUtil = new OAuthUtil();
            string timeStamp = oauthUtil.GenerateTimeStamp();
            string nonce = oauthUtil.GenerateNonce();
            string normalizedUrl; string normalizedRequestParameters;

  
            string signature = oauthUtil.GenerateSignature(uri, consumerKey, consumerSecret, token, tokenSecret,
                httpMethod.ToUpper(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
           
            signature = System.Web.HttpUtility.UrlEncode(signature);
            
            StringBuilder sb = new StringBuilder();
            sb.Append("Authorization: OAuth realm=\"\",oauth_version=\"1.0\",");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", consumerKey);
            if (!String.IsNullOrEmpty(token))
            {
                token = System.Web.HttpUtility.UrlEncode(token);
                sb.AppendFormat("oauth_token=\"{0}\",", token);
            }
            sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            return sb.ToString();
        }
Example #2
0
        public static string GenerateHeader(Uri uri, String consumerKey, String consumerSecret, String token, String tokenSecret, String httpMethod)
        {
            OAuthUtil oauthUtil = new OAuthUtil();
            string    timeStamp = oauthUtil.GenerateTimeStamp();
            string    nonce = oauthUtil.GenerateNonce();
            string    normalizedUrl; string normalizedRequestParameters;


            string signature = oauthUtil.GenerateSignature(uri, consumerKey, consumerSecret, token, tokenSecret,
                                                           httpMethod.ToUpper(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);

            signature = System.Web.HttpUtility.UrlEncode(signature);

            StringBuilder sb = new StringBuilder();

            sb.Append("Authorization: OAuth realm=\"\",oauth_version=\"1.0\",");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", consumerKey);
            if (!String.IsNullOrEmpty(token))
            {
                token = System.Web.HttpUtility.UrlEncode(token);
                sb.AppendFormat("oauth_token=\"{0}\",", token);
            }
            sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            return(sb.ToString());
        }
        /// <summary>
        /// Takes an existing httpwebrequest and modifies its headers according to
        /// the authentication system used.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override void ApplyAuthenticationToRequest(HttpWebRequest request)
        {
            base.ApplyAuthenticationToRequest(request);

            string oauthHeader = OAuthUtil.GenerateHeader(
                request.RequestUri,
                request.Method,
                parameters);

            request.Headers.Add(oauthHeader);
        }
        /// <summary>
        /// Takes an existing httpwebrequest and modifies its headers according to
        /// the authentication system used.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override void ApplyAuthenticationToRequest(HttpWebRequest request)
        {
            base.ApplyAuthenticationToRequest(request);

            if (!string.IsNullOrEmpty(parameters.AccessCode) && string.IsNullOrEmpty(parameters.AccessToken))
            {
                OAuthUtil.GetAccessToken(parameters);
            }

            request.Headers.Set("Authorization", String.Format(
                                    "{0} {1}", parameters.TokenType, parameters.AccessToken));
        }
        /// <summary>
        /// Takes an existing httpwebrequest and modifies it's headers according to
        /// the authentication system used.
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public override void ApplyAuthenticationToRequest(HttpWebRequest request)
        {
            base.ApplyAuthenticationToRequest(request);

            string oauthHeader = OAuthUtil.GenerateHeader(request.RequestUri,
                                                          this.ConsumerKey,
                                                          this.ConsumerSecret,
                                                          this.Token,
                                                          this.TokenSecret,
                                                          request.Method);

            request.Headers.Add(oauthHeader);
        }
Example #6
0
        /////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////
        /// <summary>sets up the correct credentials for this call</summary>
        //////////////////////////////////////////////////////////////////////
        protected override void EnsureCredentials()
        {
            HttpWebRequest http = this.Request as HttpWebRequest;

            if (string.IsNullOrEmpty(this.factory.ConsumerKey) || string.IsNullOrEmpty(this.factory.ConsumerSecret))
            {
                throw new GDataRequestException("ConsumerKey and ConsumerSecret must be provided to use GOAuthRequestFactory");
            }

            string oauthHeader = OAuthUtil.GenerateHeader(http.RequestUri,
                                                          this.factory.ConsumerKey,
                                                          this.factory.ConsumerSecret,
                                                          this.factory.Token,
                                                          this.factory.TokenSecret,
                                                          http.Method);

            this.Request.Headers.Remove("Authorization"); // needed?
            this.Request.Headers.Add(oauthHeader);
        }
 public override void Execute()
 {
     try {
         base.Execute();
     } catch (GDataRequestException re) {
         HttpWebResponse webResponse = re.Response as HttpWebResponse;
         if (webResponse != null && webResponse.StatusCode == HttpStatusCode.Unauthorized)
         {
             Tracing.TraceMsg("Access token might have expired, refreshing.");
             Reset();
             try {
                 OAuthUtil.RefreshAccessToken(this.factory.Parameters);
             } catch (WebException e) {
                 Tracing.TraceMsg("Failed to refresh access token: " + e.StackTrace);
                 throw re;
             }
             base.Execute();
         }
         else
         {
             throw;
         }
     }
 }
        public static string GenerateHeader(Uri uri, String consumerKey, String consumerSecret, String token, String tokenSecret, String httpMethod)
        {
            OAuthUtil oauthUtil = new OAuthUtil();
            string    timeStamp = oauthUtil.GenerateTimeStamp();
            string    nonce     = oauthUtil.GenerateNonce();

            string signature = oauthUtil.GenerateSignature(uri, consumerKey, consumerSecret, token, tokenSecret,
                                                           httpMethod.ToUpper(), timeStamp, nonce);

            StringBuilder sb = new StringBuilder();

            sb.Append("Authorization: OAuth oauth_version=\"1.0\",");
            sb.AppendFormat("oauth_nonce=\"{0}\",", EncodingPerRFC3986(nonce));
            sb.AppendFormat("oauth_timestamp=\"{0}\",", EncodingPerRFC3986(timeStamp));
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", EncodingPerRFC3986(consumerKey));
            if (!String.IsNullOrEmpty(token))
            {
                sb.AppendFormat("oauth_token=\"{0}\",", EncodingPerRFC3986(token));
            }
            sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
            sb.AppendFormat("oauth_signature=\"{0}\"", EncodingPerRFC3986(signature));

            return(sb.ToString());
        }
Example #9
0
        /// <summary>
        /// Generates an OAuth header.
        /// </summary>
        /// <param name="uri">The URI of the request</param>
        /// <param name="consumerKey">The consumer key</param>
        /// <param name="consumerSecret">The consumer secret</param>
        /// <param name="token"></param>
        /// <param name="httpMethod">The http method</param>
        /// <returns>The OAuth authorization header</returns>
        private static string GenerateHeader(Uri uri, String consumerKey, String consumerSecret, string token, String httpMethod)
        {
            Contract.Assume(!string.IsNullOrEmpty(token));
            //token = System.Web.HttpContext.Current.Server.UrlEncode(token);
            //string accessecret =
            OAuthUtil oauthUtil = new OAuthUtil();
            string timeStamp = oauthUtil.GenerateTimeStamp();
            string nonce = oauthUtil.GenerateNonce();
            string normalizedUrl; string normalizedRequestParameters;

            string signature = oauthUtil.GenerateSignature(uri, consumerKey, consumerSecret, UrlEncode(token), MvcApplication.GoogleTokenManager.GetTokenSecret(token),
                httpMethod.ToUpper(), timeStamp, nonce, out normalizedUrl, out normalizedRequestParameters);
            signature = UrlEncode(signature);

            //StringBuilder sb = new StringBuilder();
            //sb.Append("Authorization: OAuth realm=\"\",oauth_version=\"1.0\",");
            //sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            //sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            //sb.AppendFormat("oauth_consumer_key=\"{0}\",", consumerKey);
            //sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
            //sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            StringBuilder sb = new StringBuilder();
            sb.Append("Authorization: OAuth oauth_version=\"1.0\",");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", consumerKey);
            sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
            sb.AppendFormat("oauth_token=\"{0}\",",  UrlEncode(token));
               // sb.AppendFormat("oauth_token_secret=\"{0}\",", MvcApplication.GoogleTokenManager.GetTokenSecret(State.GoogleAccessToken));
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            return sb.ToString();
        }