public HipchatGenerateTokenResponse GenerateToken( GrantType grantType, IEnumerable<TokenScope> scopes, string username = null, string code = null, string redirectUri = null, string password = null, string refreshToken = null) { using (JsonSerializerConfigScope()) { var request = new GenerateTokenRequest { Username = username, Code = code, GrantType = grantType, Password = password, RedirectUri = redirectUri, RefreshToken = refreshToken, Scope = string.Join(" ", scopes.Select(x => x.ToString())) }; try { return HipchatEndpoints.GenerateTokenEndpoint .AddHipchatAuthentication(_authToken) .PostJsonToUrl(request) .FromJson<HipchatGenerateTokenResponse>(); } catch (Exception exception) { if (exception is WebException) throw ExceptionHelpers.WebExceptionHelper(exception as WebException, ""); throw ExceptionHelpers.GeneralExceptionHelper(exception, "GenerateToken"); } } }
/// <summary> /// Gets an OAuth token for requested grant type. /// </summary> /// <param name="grantType">The type of grant request</param> /// <param name="scopes">List of scopes that is requested</param> /// <param name="basicAuthUsername">If you supply this, the basicAuthUsername and basicAuthPassword will be passed as credentials in BasicAuthentication format. (Needed to generate a token for an addon)</param> /// <param name="basicAuthPassword">If you supply this, the basicAuthUsername and basicAuthPassword will be passed as credentials in BasicAuthentication format. </param> /// <param name="username">The user name to generate a token on behalf of. Only valid in /// the 'Password' and 'ClientCredentials' grant types.</param> /// <param name="code">The authorization code to exchange for an access token. Only valid in the 'AuthorizationCode' grant type</param> /// <param name="redirectUri">The Url that was used to generate an authorization code, and it must match that value. Only valid in the 'AuthorizationCode' grant.</param> /// <param name="password">The user's password to use for authentication when creating a token. Only valid in the 'Password' grant.</param> /// <param name="refreshToken">The refresh token to use to generate a new access token. Only valid in the 'RefreshToken' grant.</param> public HipchatGenerateTokenResponse GenerateToken( GrantType grantType, IEnumerable<TokenScope> scopes, string basicAuthUsername = null, string basicAuthPassword = null, string username = null, string code = null, string redirectUri = null, string password = null, string refreshToken = null) { using (JsonSerializerConfigScope()) { var request = new GenerateTokenRequest { Username = username, Code = code, GrantType = grantType, Password = password, RedirectUri = redirectUri, RefreshToken = refreshToken, Scope = string.Join(" ", scopes.Select(x => x.ToString())) }; Action<HttpWebRequest> requestFilter = x => { }; if (!basicAuthUsername.IsEmpty() && !basicAuthPassword.IsEmpty()) { var auth = string.Format("{0}:{1}", basicAuthUsername, basicAuthPassword); var encrypted = Convert.ToBase64String(Encoding.ASCII.GetBytes(auth)); var creds = string.Format("{0} {1}", "Basic", encrypted); requestFilter = x => x.Headers[HttpRequestHeader.Authorization] = creds; } var endpoint = HipchatEndpoints.GenerateTokenEndpoint; var form = request.FormEncodeHipchatRequest(); try { var response = endpoint .PostToUrl(request.FormEncodeHipchatRequest(), requestFilter: requestFilter) .FromJson<HipchatGenerateTokenResponse>(); return response; } catch (Exception exception) { if (exception is WebException) throw ExceptionHelpers.WebExceptionHelper(exception as WebException, ""); throw ExceptionHelpers.GeneralExceptionHelper(exception, "GenerateToken"); } } }