private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); switch (response.Status) { // Refresh the access token if the status is "Unauthorized" (HTTP Status Code 401: Unauthorized) // This will only be attempted once as refresh tokens are single use case ResponseStatus.Unauthorized: response = await RetryExpiredTokenRequest <T>(request).ConfigureAwait(false); break; // Continue to retry the request if the status is "Pending" (HTTP Status Code 202: Approved) // this will occur if a preview/thumbnail is not ready yet case ResponseStatus.Pending: response = await ExecuteRequest <T>(request, queueRequest).ConfigureAwait(false); break; } return(response); }
public virtual async Task <OAuthSession> AuthenticateBoxDeveloperEditionAsync() { string assertion = JWTConstructAssertion(); BoxRequest boxRequest = new BoxRequest(_config.BoxApiDeveloperEditionTokenUri) .Method(RequestMethod.Post) .Header(Constants.RequestParameters.UserAgent, _config.UserAgent) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.JWT) .Payload(Constants.RequestParameters.Assertion, assertion) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); var session = boxResponse.ResponseObject; using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = session; OnSessionAuthenticated(session); } return(session); }
public async Task <OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) { throw new ArgumentException("Auth code cannot be null or empty", "authCode"); } BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.AuthorizationCode) .Payload(Constants.RequestParameters.Code, authCode) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) { Session = boxResponse.ResponseObject; var handler = SessionAuthenticated; if (handler != null) { handler(this, new SessionAuthenticatedEventArgs(Session)); } } return(boxResponse.ResponseObject); }
private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); if (response.Status == ResponseStatus.Unauthorized) { // Refresh the access token if the status is "Unauthorized" (HTTP Status Code 401: Unauthorized) // This will only be attempted once as refresh tokens are single use response = await RetryExpiredTokenRequest <T>(request).ConfigureAwait(false); } return(response); }
private async Task <IBoxResponse <T> > ExecuteRequest <T>(IBoxRequest request, bool queueRequest) where T : class { var response = queueRequest ? await _service.EnqueueAsync <T>(request).ConfigureAwait(false) : await _service.ToResponseAsync <T>(request).ConfigureAwait(false); switch (response.Status) { // Continue to retry the request if the status is "Pending" (HTTP Status Code 202: Approved) // this will occur if a preview/thumbnail is not ready yet case ResponseStatus.Pending: response = await ExecuteRequest <T>(request, queueRequest).ConfigureAwait(false); break; } return(response); }
/// <summary> /// Performs the authentication request using the provided auth code /// </summary> /// <param name="authCode"></param> /// <returns></returns> protected async Task <OAuthSession> ExchangeAuthCode(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) { throw new ArgumentException("Auth code cannot be null or empty", "authCode"); } BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.AuthorizationCode) .Payload(Constants.RequestParameters.Code, authCode) .Payload(Constants.RequestParameters.ClientId, _config.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _config.ClientSecret) .Payload(Constants.RequestParameters.BoxDeviceId, _config.DeviceId) .Payload(Constants.RequestParameters.BoxDeviceName, _config.DeviceName); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); return(boxResponse.ResponseObject); }
public async Task <OAuthSession> AuthenticateAsync(string authCode) { if (string.IsNullOrWhiteSpace(authCode)) { throw new ArgumentException("Auth code cannot be null or empty", "authCode"); } BoxRequest boxRequest = new BoxRequest(_config.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload("grant_type", "authorization_code") .Payload("code", authCode) .Payload("client_id", _config.ClientId) .Payload("client_secret", _config.ClientSecret); IBoxResponse <OAuthSession> boxResponse = await _service.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(_converter); using (await _mutex.LockAsync().ConfigureAwait(false)) Session = boxResponse.ResponseObject; return(boxResponse.ResponseObject); }
private async Task <OAuthSession> CCGAuthPostAsync(string subType, string subId) { BoxRequest boxRequest = new BoxRequest(_boxConfig.BoxApiHostUri, Constants.AuthTokenEndpointString) .Method(RequestMethod.Post) .Payload(Constants.RequestParameters.GrantType, Constants.RequestParameters.ClientCredentials) .Payload(Constants.RequestParameters.ClientId, _boxConfig.ClientId) .Payload(Constants.RequestParameters.ClientSecret, _boxConfig.ClientSecret) .Payload(Constants.RequestParameters.SubjectType, subType) .Payload(Constants.RequestParameters.SubjectId, subId); var converter = new BoxJsonConverter(); IBoxResponse <OAuthSession> boxResponse = await _boxService.ToResponseAsync <OAuthSession>(boxRequest).ConfigureAwait(false); boxResponse.ParseResults(converter); return(boxResponse.ResponseObject); }