Пример #1
0
        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(_factory.Parameters);
                    }
                    catch (WebException e)
                    {
                        Tracing.TraceMsg("Failed to refresh access token: " + e.StackTrace);
                        throw re;
                    }

                    base.Execute();
                }
                else
                {
                    throw;
                }
            }
        }
        /// <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));
        }
Пример #4
0
        /// <summary>
        /// sets up the correct credentials for this call.
        /// </summary>
        protected override void EnsureCredentials()
        {
            HttpWebRequest http = Request as HttpWebRequest;

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

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

            Request.Headers.Remove("Authorization"); // needed?
            Request.Headers.Add(oauthHeader);
        }