@@ -254,10 +254,13 @@ public async Task<string> AuthorizeAsync()
254254
255255 try
256256 {
257+
257258 // Build the base authorization URL using the correct OAuth hostname
258259 // Transform api.contentstack.io -> app.contentstack.com for OAuth authorization
259260 var oauthHost = GetOAuthHost ( GetClient ( ) . contentstackOptions . Host ) ;
261+
260262 var baseUrl = $ "https://{ oauthHost } /#!/apps/{ _options . AppId } /authorize";
263+
261264 var authUrl = new UriBuilder ( baseUrl ) ;
262265
263266 // Add required OAuth parameters
@@ -310,10 +313,21 @@ public async Task<OAuthTokens> ExchangeCodeForTokenAsync(string authorizationCod
310313
311314 try
312315 {
316+
313317 // Create the OAuth token service for authorization code exchange
314318 OAuthTokenService tokenService ;
315319
316- if ( _options . UsePkce && ! string . IsNullOrEmpty ( this . codeVerifier ) )
320+ if ( _options . UsePkce )
321+ {
322+ // PKCE code verifier should be available from the instance
323+ if ( string . IsNullOrEmpty ( this . codeVerifier ) )
324+ {
325+ throw new Exceptions . OAuthConfigurationException (
326+ "PKCE code verifier not found. Make sure to call AuthorizeAsync() before ExchangeCodeForTokenAsync()." ) ;
327+ }
328+ }
329+
330+ if ( _options . UsePkce && ! string . IsNullOrEmpty ( this . codeVerifier ) )
317331 {
318332 tokenService = OAuthTokenService . CreateForAuthorizationCode (
319333 serializer : GetClient ( ) . serializer ,
@@ -557,9 +571,18 @@ private static string GetOAuthHost(string baseHost)
557571 if ( string . IsNullOrEmpty ( baseHost ) )
558572 return baseHost ;
559573
560- // Transform api.contentstack.io -> app.contentstack.com
574+ // Extract hostname from URL if it contains protocol
561575 var oauthHost = baseHost ;
562-
576+ if ( oauthHost . StartsWith ( "https://" ) )
577+ {
578+ oauthHost = oauthHost . Substring ( 8 ) ; // Remove "https://"
579+ }
580+ else if ( oauthHost . StartsWith ( "http://" ) )
581+ {
582+ oauthHost = oauthHost . Substring ( 7 ) ; // Remove "http://"
583+ }
584+
585+ // Transform api.contentstack.io -> app.contentstack.com
563586 // Replace .io with .com
564587 if ( oauthHost . EndsWith ( ".io" ) )
565588 {
@@ -653,9 +676,9 @@ private async Task RevokeOauthAppAuthorizationAsync(string authorizationId)
653676 // Make the API call to revoke authorization
654677 var response = await GetClient ( ) . InvokeAsync < OAuthAppRevocationService , ContentstackResponse > ( service ) ;
655678 }
656- catch
679+ catch ( Exception ex )
657680 {
658- throw ;
681+ throw ex ;
659682 }
660683 finally
661684 {
0 commit comments