【问题标题】:Not able to authenticate Google Account in Spyder无法在 Spyder 中验证 Google 帐户
【发布时间】:2020-04-22 04:31:04
【问题描述】:

我正在尝试使用 Spyder 在 spyder python 脚本中运行 read_gbq,我可以单击授权 URL 并粘贴授权代码,但在按下 shift+enter 后它会进入 bad handshake。任何帮助将不胜感激。

附上相同的截图:

spyder中使用的Python代码:

import pandas_gbq
import pandas as pd
project_id ='<'project_id'>
sql = 'select * from <datasetname.tablename> limit 10'

df = pandas_gbq.read_gbq(sql,project_id,reauth=True,dialect='standard')
print('DONE Connecting with BQ')

【问题讨论】:

    标签: python-3.x oauth-2.0 google-bigquery google-oauth spyder


    【解决方案1】:

    pandas-gbq 使用Google BigQuery service via OAuth 2.0 进行身份验证。使用 credentials 参数显式传入 Google 凭据。

    如果没有设置凭据参数,pandas-gbq 会尝试以下身份验证方法:

    内存中,缓存在pandas_gbq.context.credentials 的凭据。这些凭据通过调用pandas_gbq.read_gbq()pandas_gbq.to_gbq() 自动缓存在内存中。要手动设置凭据,请构造一个 google.auth.credentials.Credentials 对象并将其设置为上下文凭据,如下例所示。有关获取凭据的更多信息,请参阅auth documentation

    import pandas_gbq
    from google.oauth2 import service_account
    credentials = service_account.Credentials.from_service_account_file(
        '/path/to/key.json',
    )
    pandas_gbq.context.credentials = credentials
    pandas_gbq.context.project = "your-project-id"
    
    # The credentials and project_id arguments can be omitted.
    df = pandas_gbq.read_gbq("SELECT my_col FROM `my_dataset.my_table`")
    

    我认为问题可能出在 Google 凭据必须使用凭据参数专门传入这一事实。关注 pandas-gbq authentication documentation

    【讨论】:

    • 此示例利用 service_account 凭据,在我当前的情况下,由于缺少权限,我无法创建服务帐户凭据。是否可以在 json 中提供 google 帐户的用户名/密码并将其传递给凭据参数?
    • 有一些导致这种情况,但都不是图书馆的错。我会检查,没有特别的顺序:1. Python 版本,2. openssl 版本,旧版本可能缺乏 SNI 支持,3. 请求和证书的版本。您可以尝试安装requests[security] 看看它是否会自动修复它。例如,某些 VM 包含需要更新的非常旧的 SSL 根证书(有时在服务器上更新 SSL 可以解决问题)。 JSON 私钥文件仅用于服务帐户 google-auth.readthedocs.io/en/latest/reference/…
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 2022-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2016-12-15
    • 2018-03-01
    相关资源
    最近更新 更多