【问题标题】:No module named "requests" when trying to use google OAuth2 with Docker尝试将 google OAuth2 与 Docker 一起使用时没有名为“请求”的模块
【发布时间】:2018-12-03 00:29:16
【问题描述】:

我正在尝试将 google OAuth 用于我的网络应用程序。为此,我在我的 venv 和 Docker 构建期间(来自 requirements.txt)安装了包 google-api-python-client 和 google-auth。尽管如此,当我运行我的应用程序时,它找不到请求模块,抱怨:

flask.cli.NoAppException: While importing "debateit", an ImportError was raised:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/google/auth/transport/requests.py", line 23, in <module>
    import requests
ImportError: No module named 'requests'

导入如下:

from google.auth.transport import requests

并且使用如下:

idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])

id_token.verify_oauth2_token 等其他导入工作正常。

我检查了我的 docker build,它说我正确地包含了 google-auth:

Installing collected packages: ... google-auth, httplib2, google-auth-httplib2, google-api-python-client
Successfully installed ... google-api-python-client-1.7.3 google-auth-1.5.0 google-auth-httplib2-0.0.3 httplib2-0.11.3 ...

当我查看 venv 时,我可以清楚地看到 google.auth.transport.requests 模块,它只是在应用程序本身中不起作用。

我错过了什么?什么可能导致找不到此模块?

【问题讨论】:

    标签: python docker google-authentication


    【解决方案1】:

    所以我发现了问题所在 - 在 google.auth.transport.requests 模块中,他们尝试导入库“请求”。我没有安装这个库。我已经这样做了,现在可以了。

    我遵循的指南:https://developers.google.com/identity/sign-in/web/backend-auth 没有提到您需要安装此库。我误解了 requests 模块中请求的导入应该做什么。

    【讨论】:

    • AppEngine 文档:10 分钟编写代码,3 小时设置配置...对于后端验证示例,您需要安装“请求”和“谷歌验证”。像每个外部库(是的,google-auth 是外部的)一样,您应该将它们 pip-install 到lib/(有些人使用libs/)。最好的方法是在一个新行(“requests”、“google-auth”)中创建一个 requirements.txt 文件,然后调用 mkdir libpip install -t lib/ -r requirements.txt
    • 大多数人使用 virtualenv 来确保本地 pip/python 版本和应用引擎 pip/python 版本之间没有区别。如果您对 pip-install 有问题,请尝试添加 setup.cfg 文件:[install]\nprefix= AppEngine 的文档也没有提到您需要将以下内容添加到您的 app.yaml:libraries:\n- name: ssl\n version: latest\n 这会安装一些本地库对于 auth 调用的 ssl(不确定是哪个)。我觉得很有趣,您无法通过配置 app.yaml 文件来安装google-auth 本身...
    • (stackoverflow 不允许我在 cmets 中写换行符,所以我用 '\n' 标记它们)
    • 在 google-auth==1.30.0 和 python 3.7 上的工作就像一个魅力。谢谢。
    【解决方案2】:

    正如the documentation 中所报告的,它应该更有可能是:

    import google.auth.transport.requests
    import requests
    
    request = google.auth.transport.requests.Request()
    
    credentials.refresh(request)
    

    但出于您的目的,我会建议:

    from google.auth.transport.requests import Request
    

    然后更改以下内容:

    idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
    

    到:

    idinfo = id_token.verify_oauth2_token(token, Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
    

    这是一个错误,因为路径 google.auth.transport.requests 内没有名为 requests 的函数或类。

    我的建议是基于这条线

    idinfo = id_token.verify_oauth2_token(token, requests.Request(), app.config["GOOGLE_LOGIN_CLIENT_ID"])
    

    这表明您使用了一个名为 Requests() 的类,该类存在于 google.auth.transport.requests 中,正如您在 the documentation 中看到的那样。

    【讨论】:

    • 不幸的是,我更新了代码以符合您的建议并遇到了完全相同的问题,没有名为 requests 的模块。我将 bash 添加到我的 web 图像中并检查包是否存在 - 确实存在。这里出了点问题...
    【解决方案3】:

    我知道这已得到解答,但您可以通过运行以下命令快速全局安装这些库:

    pip install google-auth
    

    如果有人不想全局安装 pip 或在本地虚拟环境中安装库,这里是如何安装它们: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

    【讨论】:

      【解决方案4】:

      pip install requests

      似乎google.auth.transport.requests 使用了requests

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-04
        • 1970-01-01
        • 2021-10-13
        • 1970-01-01
        • 2015-12-23
        • 2017-12-08
        • 2019-08-11
        相关资源
        最近更新 更多