Пример #1
0
        /// <summary>
        /// Gets the request token from the remote server.
        /// </summary>
        public virtual void GetRequestToken()
        {
            this.Authorization.PrepareForRequestToken();

            if (!String.IsNullOrEmpty(this.ReturnUrl))
            {
                this.Authorization.Callback = String.Format
                                              (
                    "{0}{1}state={2}",
                    this.ReturnUrl,
                    (this.ReturnUrl.Contains("?") ? "&" : "?"),
                    this.State
                                              );
            }

            _RequestToken = new OAuthRequestToken
                            (
                OAuthUtility.Post
                (
                    this.RequestTokenUrl,
                    authorization: this.Authorization
                ),
                this.AuthorizeUrl,
                this.Parameters
                            );
        }
Пример #2
0
        /// <summary>
        /// Gets the access token from the remote server.
        /// </summary>
        protected override void GetAccessToken()
        {
            // authorization code is required for request
            if (String.IsNullOrEmpty(this.AuthorizationCode))
            {
                throw new ArgumentNullException("AuthorizationCode");
            }

            // set default access tken value
            this.AccessToken = Nemiro.OAuth.AccessToken.Empty;

            // prepare
            this.Authorization.PrepareForAccessToken();

            // set request data
            this.Authorization.Verifier    = this.AuthorizationCode;
            this.Authorization.Token       = this.RequestToken.OAuthToken;
            this.Authorization.TokenSecret = this.RequestToken.OAuthTokenSecret;

            // send request
            base.AccessToken = new OAuthAccessToken
                               (
                OAuthUtility.Post
                (
                    this.AccessTokenUrl,
                    authorization: this.Authorization
                )
                               );

            // remove oauth_verifier from headers
            this.Authorization.Remove("oauth_verifier");
        }
Пример #3
0
        /// <summary>
        /// Gets the request token from the remote server.
        /// </summary>
        public virtual void GetRequestToken()
        {
            this.Authorization.PrepareForRequestToken();

            if (!String.IsNullOrEmpty(this.ReturnUrl))
            {
                this.Authorization.Callback = String.Format
                                              (
                    "{0}{1}state={2}",
                    this.ReturnUrl,
                    (this.ReturnUrl.Contains("?") ? "&" : "?"),
                    this.State
                                              );
            }

            _RequestToken = new OAuthRequestToken
                            (
                OAuthUtility.Post
                (
                    this.RequestTokenUrl,
                    authorization: this.Authorization,
                    headers: new NameValueCollection {
                { "Accept", "text/plain" }
            }                                                             // application/x-www-form-urlencoded ???
                ),
                this.AuthorizeUrl,
                this.Parameters
                            );
        }
Пример #4
0
        /// <summary>
        /// Sends a request to refresh the access token.
        /// </summary>
        /// <param name="accessToken">May contain an access token, which should be refreshed.</param>
        /// <remarks>
        /// <para>If <paramref name="accessToken"/> parameter is not specified, it will use the current access token from the same property of the current class instance.</para>
        /// <para>Token must contain the <b>refresh_token</b>, which was received together with the access token.</para>
        /// </remarks>
        /// <exception cref="NotSupportedException">
        /// <para>Provider does not support refreshing the access token, or the method is not implemented.</para>
        /// <para>Use the property <see cref="OAuthBase.SupportRefreshToken"/>, to check the possibility of calling this method.</para>
        /// </exception>
        /// <exception cref="AccessTokenException">
        /// <para>Access token is not found or is not specified.</para>
        /// <para>-or-</para>
        /// <para><b>refresh_token</b> value is empty.</para>
        /// </exception>
        /// <exception cref="RequestException">Error during execution of a web request.</exception>
        public override AccessToken RefreshToken(AccessToken accessToken = null)
        {
            if (!this.SupportRefreshToken)
            {
                throw new NotSupportedException();
            }

            var token = (OAuth2AccessToken)base.GetSpecifiedTokenOrCurrent(accessToken, refreshTokenRequired: true);

            //HttpAuthorization authorization = null;
            var parameters = new NameValueCollection
            {
                { "client_id", this.ApplicationId },
                { "client_secret", this.ApplicationSecret },
                { "grant_type", GrantType.RefreshToken },
                { "refresh_token", token.RefreshToken }
            };

            /*if (!String.IsNullOrEmpty(token.TokenType) && token.TokenType.Equals(AccessTokenType.Bearer, StringComparison.OrdinalIgnoreCase))
             * {
             * authorization = new HttpAuthorization(AuthorizationType.Bearer, accessToken.Value);
             * }
             * else
             * {
             * parameters.Add("access_token", accessToken.Value);
             * }*/

            var result = OAuthUtility.Post
                         (
                this.AccessTokenUrl,
                parameters: parameters,
                accessToken: token
                //authorization: authorization
                         );

            return(new OAuth2AccessToken(result));
        }
Пример #5
0
        // [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]

        /// <summary>
        /// Gets the access token from the remote server.
        /// </summary>
        protected override void GetAccessToken()
        {
            // authorization code is required for request
            if (this.GrantType.IsAuthorizationCode && String.IsNullOrEmpty(this.AuthorizationCode))
            {
                throw new ArgumentNullException("AuthorizationCode");
            }
            else if (this.GrantType.IsPassword || this.GrantType.IsClientCredentials)
            {
                if (String.IsNullOrEmpty(this.Username))
                {
                    throw new ArgumentNullException("username");
                }
                if (String.IsNullOrEmpty(this.Password))
                {
                    throw new ArgumentNullException("password");
                }
            }

            // set default access token value
            this.AccessToken = Nemiro.OAuth.AccessToken.Empty;

            // set request data
            HttpAuthorization   auth       = null;
            NameValueCollection parameters = new NameValueCollection();

            if (this.GrantType.IsAuthorizationCode)
            {
                // http://tools.ietf.org/html/rfc6749#section-4.1.3
                parameters.Add("code", this.AuthorizationCode);
            }
            else if (this.GrantType.IsPassword)
            {
                // http://tools.ietf.org/html/rfc6749#section-4.3.2
                parameters.Add("username", this.Username);
                parameters.Add("password", this.Password);
            }
            else if (this.GrantType.IsClientCredentials)
            {
                // http://tools.ietf.org/html/rfc6749#section-4.4.2
                auth = new HttpAuthorization(AuthorizationType.Basic, OAuthUtility.ToBase64String("{0}:{1}", this.Username, this.Password));
            }
            else
            {
                throw new NotSupportedException(String.Format("GrantType '{0}' is not supported. Please write the code ;)", this.GrantType));
            }

            parameters.Add("client_id", this.ApplicationId);
            parameters.Add("client_secret", this.ApplicationSecret);
            parameters.Add("grant_type", this.GrantType);


            if (!String.IsNullOrEmpty(this.ReturnUrl))
            {
                parameters.Add("redirect_uri", this.ReturnUrl);
            }

            var result = OAuthUtility.Post
                         (
                this.AccessTokenUrl,
                parameters,
                auth
                         );

            if (result.ContainsKey("error"))
            {
                this.AccessToken = new AccessToken(new ErrorResult(result));
            }
            else
            {
                this.AccessToken = new OAuth2AccessToken(result);
            }
        }