首页 > 解决方案 > 使用 Azure AD 身份验证连接到雪花的 SQLAlchemy

问题描述

我正在尝试使用以下代码使用 SQL alchemy 连接到我的雪花帐户。它不起作用,我这是因为我们使用 Azure AD 身份验证登录。任何帮助表示赞赏。

from sqlalchemy import create_engine
    
    engine = create_engine(
        'snowflake://{user}:{password}@{account}/'.format(
            user='xxx',
            password='xxx',
            account='xxx'
            
        )
    )
    try:
        connection = engine.connect()
        results = connection.execute('select current_version()').fetchone()
        print(results[0])
    finally:
        connection.close()
        engine.dispose()

错误信息:

DatabaseError: (snowflake.connector.errors.DatabaseError) 250001 (08001): Failed to connect to DB: QB67303.eu-west-1.snowflakecomputing.com:443. Incorrect username or password was specified.
(Background on this error at: http://sqlalche.me/e/13/4xp6)

标签: sqlalchemyazure-active-directorysnowflake-cloud-data-platform

解决方案


虽然这不是使用 sqlalchemy 而是使用本机雪花 sql 连接器,但这对我有用:

import snowflake.connector

ctx = snowflake.connector.connect(
    user='<YOUR_USERNAME>',
    account='<YOUR_ACCOUNT ; not always needed>',
    host='<YOUR SF host, ex: xxxxxx.snowflakecomputing.com / xxxxxx.a0.satoricyber.net>',
    authenticator='externalbrowser',
    warehouse='<optional>'
    )
cs = ctx.cursor()
try:
    cs.execute("SELECT * FROM ORGDATA.PUBLIC.CUSTOMERS")
    rows = cs.fetchall()
    for row in rows:
        print(row)
finally:
    cs.close()
ctx.close()

推荐阅读