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