OAuthException

View as Markdown

OAuthException

OAuthException is the exception the SDK throws when an Open Authorization (OAuth) step fails, in the Contentstack.Management.Core.Exceptions namespace. Four subclasses name the step that failed.

When to Use This Class: Catch it around the methods of OAuthHandler, and around async request methods on a client that uses an OAuth access token.

Conceptual Role: Acts as the error type of the OAuth flow. It extends ContentstackException.

Returns: OAuthException. An OAuth flow error.

Class-Level Notes

The class hierarchy. The five classes below form one tree, so catch (OAuthException) catches all of them:

ClassParentThe SDK throws it when
OAuthExceptionContentstackExceptionNo tokens are stored, a redirect URL has no code, the app has no authorization for the user, or a token request fails. Also when the automatic token refresh of an async request fails.
OAuthConfigurationExceptionOAuthExceptionOAuthOptions fails validation, or ExchangeCodeForTokenAsync runs for a PKCE app before AuthorizeAsync on the same handler.
OAuthAuthorizationExceptionOAuthExceptionAuthorizeAsync cannot build the authorization URL.
OAuthTokenExceptionOAuthExceptionThe SDK never throws this class itself. It is the parent of OAuthTokenRefreshException.
OAuthTokenRefreshExceptionOAuthTokenExceptionRefreshTokenAsync runs without a refresh token argument and no stored refresh token.

API rejections arrive wrapped.

  • When the token endpoint rejects a request in ExchangeCodeForTokenAsync, RefreshTokenAsync, or HandleRedirectAsync, the SDK throws OAuthException with the ContentstackErrorException as InnerException. Read StatusCode and ErrorMessage from the inner exception.
  • The SDK does not wrap an ArgumentException for an empty argument.

Constructors. Each of the five classes has the same three public constructors:

ConstructorMessage
()A default text per class, such as OAuth operation failed.
(string message)The text you pass.
(string message, Exception innerException)The text you pass, with the exception that caused it.

Class-Level Properties

The OAuth exception classes add no properties. Read Message and InnerException from System.Exception.

Method Index

The OAuth exception classes have no methods besides their constructors, which the table above lists.

Class-Level Snippet

This snippet exchanges an authorization code and tells a configuration problem apart from an API rejection.

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

ContentstackClient client = new ContentstackClient();
OAuthHandler oauth = client.OAuth(new OAuthOptions() { AppId = "<APP_ID>", ClientId = "<CLIENT_ID>", RedirectUri = "<REDIRECT_URI>" });

try
{
    string authorizationUrl = await oauth.AuthorizeAsync();
    Console.WriteLine($"Open {authorizationUrl}, then paste the code from the redirect.");
    await oauth.ExchangeCodeForTokenAsync(Console.ReadLine() ?? "");
}
catch (OAuthConfigurationException e)
{
    Console.WriteLine($"Fix the OAuth settings: {e.Message}");
}
catch (OAuthException e) when (e.InnerException is ContentstackErrorException apiError)
{
    Console.WriteLine($"Request failed: {(int)apiError.StatusCode} {apiError.ErrorCode} {apiError.ErrorMessage}");
}
catch (OAuthException e)
{
    Console.WriteLine($"OAuth failed: {e.Message}");
}
catch (ArgumentException e)
{
    // Fires when the pasted code is empty
    Console.WriteLine(e.Message);
}