Search code examples
pythonsingle-sign-onsnowflake-cloud-data-platformgoogle-colaboratoryokta

How to connect python to Snowflake using SSO (sqlalchemy)?


I recently had to enable SSO with Okta and had a few python projects I was running in Google Colab.

I am trying to redesign the connection string but can't seem to get it right.

This was my initial connection string before SSO:

from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine

engine = create_engine(URL(
    account = acc,
    user = usr,
    password = psw,
    warehouse = whs,
    role = rol
))
engine.connect()

This is what I found from research it should be with SSO:

from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine

engine = create_engine(URL(
    account = acc,
    user = usr,
    password = psw,
    warehouse = whs,
    role = rol
),
connect_args={
        'authenticator': 'https://myokta.okta.com/',
        }
)
engine.connect()

I tried that but I am getting this error: enter image description here

I also tried using {'authenticator': 'externalbrowser'} but because I am in Google Colab I get an error stating Unable to open a browser in this environment..

The Web UI is working for the same user so it's just in Colab that I am having this issue.

How should I go about to connect?

EDIT: So after doing some research I found that because we have MFA enabled this would not work. Is it possible to then use:

engine = create_engine(URL(
    account = acc,
    user = usr,
    warehouse = whs,
    role = rol,
    authenticator = 'externalbrowser'
))
engine = engine.connect()

And have the externalbrowser be an iframe in the same notebook?


Solution

  • I managed to find the solution. When running engine.connect() with authenticator='externalbrowser' and Google Collab cannot open a separate tab, it will provide a manual link, which, when clicked, opens another tab pointing to localhost URL with a token as a param. I then copy this URL and when going back to the notebook, I paste this URL to the input box opened in the cell.