Google Cloud Storage使用google-auth在Django中的身份验证问题。

huangapple go评论93阅读模式
英文:

Google Cloud Storage Authentication Issue with Django using google-auth

问题

我在尝试在Django中使用Google Cloud Storage时遇到了身份验证问题。我已经设置了我的Django项目,使用google-auth库来与其他Google API进行身份验证,但是当我尝试使用Google Cloud Storage时,我收到以下错误信息:

此库仅支持来自google-auth-library-python的凭据。请参阅https://google-auth.readthedocs.io/en/latest/以获取有关使用此库进行身份验证的帮助。

已采取的步骤:

  1. 我已经设置了我的Django项目,包括安装google-auth库所需的配置。
  2. 我在我的Django设置中定义了GS_CREDENTIALS变量,以存储服务帐户JSON文件的路径。
  3. 我在Django设置中使用了storages.backends.gcloud.GoogleCloudStorage存储后端来处理Google Cloud Storage上的媒体和静态文件存储。

Django settings.py

GS_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'

期望行为:

我期望google-auth库能够处理与Google Cloud Storage的身份验证,就像它在我的Django项目中与其他Google API一样。

实际行为:

尝试使用Google Cloud Storage时,会引发上述错误,表明该库仅支持来自google-auth-library-python的凭据,尽管我已经安装了google-auth并且它与其他API正常工作。

附加信息:

  1. 我已经验证了google-auth已安装在我的环境中并且是最新的。
  2. 凭据已正确设置用于其他Google API,并且它们按预期工作。
  3. 我尝试过各种方法,包括不同的配置,但错误仍然存在。

系统信息:

  • Django版本:4.0.6
  • Python版本:Anaconda Python 3.9.16
  • 操作系统:MacOS 13.4 M1

我如何解决这个身份验证问题,并在使用google-auth库进行其他Google API身份验证的同时,在Django中使用Google Cloud Storage?任何有关可能导致身份验证不一致的见解或指导都将不胜感激。提前感谢您的帮助!

英文:

I am facing an authentication issue while trying to use Google Cloud Storage with Django. I have set up my Django project to use google-auth library for authentication with other Google APIs, but when I try to use Google Cloud Storage, I get the following error:

This library only supports credentials from google-auth-library-python. See https://google-auth.readthedocs.io/en/latest/ for help on authentication with this library.


Steps Taken:

  1. I have set up my Django project with the necessary configurations, including installing google-auth library.
  2. I have defined the GS_CREDENTIALS variable in my Django settings to store the path to the service account JSON file.
  3. I am using the storages.backends.gcloud.GoogleCloudStorage storage backend in Django settings to handle media and static files storage on Google Cloud Storage.

Django settings.py

GS_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
STATICFILES_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.gcloud.GoogleCloudStorage'

Expected Behavior:

I expect the google-auth library to handle the authentication with Google Cloud Storage, just as it does with other Google APIs in my Django project.

Actual Behavior:

When trying to use Google Cloud Storage, the mentioned error is raised, indicating that the library only supports credentials from google-auth-library-python, even though I have google-auth installed and working correctly with other APIs.

Additional Information:

  1. I have verified that google-auth is installed in my environment and up-to-date.
  2. The credentials are correctly set for other Google APIs, and they work as expected.
  3. I have tried various approaches, including different configurations, but the error persists.

System Information:

  • Django version: 4.0.6
  • Python version: Anaconda Python 3.9.16
  • Operating System: MacOS 13.4 M1

How can I resolve this authentication issue and use Google Cloud Storage with Django while still using the google-auth library for authentication with other Google APIs?

Any insights or guidance on what might be causing this discrepancy in authentication would be greatly appreciated. Thank you in advance for your help!

答案1

得分: 1

似乎您可能混淆了您正在使用的软件包。根据我所看到的情况,GS_CREDENTIALSdjango-storages软件包中提到,而不是在google-auth软件包中。

如果您确实在使用django-storages,请尝试在您的settings.py文件中使用以下设置:
GOOGLE_APPLICATION_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
而不是使用GS_CREDENTIALS设置。我依稀记得在某个时候尝试过这样设置,但它对我不起作用。

英文:

It seems like you may be mixing up the packages you're working with. GS_CREDENTIALS is mentioned in the django-storages package, not in the google-auth package from what I can see.

If you are indeed using django-storages, try to use the setting:
GOOGLE_APPLICATION_CREDENTIALS = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')
in your settings.py file instead of the GS_CREDENTIALS setting, I vaguely remember trying that at some point and it not working for me.

huangapple
  • 本文由 发表于 2023年7月28日 00:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定