djangooauthsnowflake-cloud-data-platformdjango-authentication

Snowflake custom OAuth not working with invalid_client error


First it was working, and it showed scope not available error, but now it is showing this error, I created the integration using the snowflake_oauth_docs and this is my query:

CREATE SECURITY INTEGRATION my_app_oauth
   TYPE = OAUTH
   ENABLED = TRUE
   OAUTH_CLIENT = CUSTOM
   OAUTH_CLIENT_TYPE = 'CONFIDENTIAL'
   OAUTH_REDIRECT_URI = 'https://b54rmx30-8000.inc1.devtunnels.ms/callback/snowflake/'
   OAUTH_ISSUE_REFRESH_TOKENS = TRUE
   OAUTH_REFRESH_TOKEN_VALIDITY = 7776000;

and then I did this DESC SECURITY INTEGRATION my_app_oauth; and this SELECT SYSTEM$SHOW_OAUTH_CLIENT_SECRETS('DATAOPSLY_OAUTH');

and I collected the client_id and client_secret

and I'm using django for my oauth, so this is my callback and login view:

def snowflake_login(request):
    # Snowflake OAuth configuration
    snowflake_client_id = 'my_client_id'
    snowflake_client_secret = 'my_secret'
    authorization_url = 'https://<my_account>.snowflakecomputing.com/oauth/authorize'
    token_url = 'https://<my_account>.snowflakecomputing.com/oauth/token'
    redirect_uri = 'https://b54rmx30-8000.inc1.devtunnels.ms/callback/snowflake/'  # Update with your Django callback URL
    scopes = 'openid email profile'  # Adjust scopes as needed

    # Redirect user to Snowflake OAuth authorization URL
    auth_params = {
        'response_type': 'code',
        'client_id': snowflake_client_id,
        'redirect_uri': redirect_uri,
        'scope': scopes,
    }
    redirect_url = f"{authorization_url}?{'&'.join([f'{k}={v}' for k, v in auth_params.items()])}"
    return redirect(redirect_url)

def snowflake_callback(request):
    # Handle callback from Snowflake OAuth
    snowflake_client_id = 'my_client_id'
    snowflake_client_secret = 'my_secret'
    token_url = 'https://<my_account>.snowflakecomputing.com/oauth/token'
    redirect_uri = 'https://b54rmx30-8000.inc1.devtunnels.ms/callback/snowflake/'  # Update with your Django callback URL

    # Get authorization code from callback request
    code = request.GET.get('code')
    
    # Exchange authorization code for access token
    token_params = {
        'grant_type': 'authorization_code',
        'code': code,
        'client_id': snowflake_client_id,
        'client_secret': snowflake_client_secret,
        'redirect_uri': redirect_uri,
    }

    # Make POST request to get access token
    response = requests.post(token_url, data=token_params)
    print("***************response:", response)
    token_data = response.json()
    print("**************token_data:", token_data)

    # Assuming successful response, store token_data as needed (e.g., in session)
    access_token = token_data.get('access_token')
    refresh_token = token_data.get('refresh_token')

    # Example of using the access token to fetch user information
    if access_token:
        headers = {
            'Authorization': f'Bearer {access_token}',
        }
        user_info_url = 'https://<my_account>.snowflakecomputing.com/oauth/userinfo'
        user_info_response = requests.get(user_info_url, headers=headers)
        user_info = user_info_response.json()

        # Example: Extract user details from user_info and create/update user in your Django app
        email = user_info.get('email')
        username = user_info.get('username')

        # Logic to authenticate user in Django (create user if not exists, login, etc.)
        # Example:
        # user, created = User.objects.get_or_create(email=email, defaults={'username': username})
        # login(request, user)

        # Redirect user to home or another page after successful login
        return redirect('home')

    # Handle error scenarios if needed
    return render(request, 'home.html', {'error_message': 'Failed to authenticate with Snowflake.'})

I have used this from requests_oauthlib import OAuth2Session

So, my problem is I receive this error:

Error occurred in authorization OAuth client integration with the given client id is not found.

even when I access the url from DESC SECURITY INTEGRATION my_app_oauth;

Firstly I tried normally and I got an error of scope not applicable or something, then, when I tried creating a new integration suddenly I received this error, don't know how to proceed further!

Can someone please help me!


Solution

  • When using the Snowflake as the OAuth server, the tokens have to be requested as:

    Url to get the authorization code:

    <OAUTH_AUTHORIZATION_ENDPOINT>?response_type=code&client_id=&redirect_uri=

    Note: We have to URL-encode the client_id and the redirect URI.

    Could you please check if you are sending the client ID after encoding?

    Refer to the following article which talks about a reference integration to get the token. Snowflake Oauth Custom