【问题标题】:Some settings seem useless in DRF Social OAuth2DRF Social OAuth2 中的某些设置似乎无用
【发布时间】:2021-10-17 17:03:00
【问题描述】:

所以我使用DRF Social Oauth2 在 DRF 中实现社交身份验证。

在设置settings.py 时,我们需要做很多事情,比如将一些应用添加到已安装的应用等。

我们还需要设置这两个设置;

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'google_client_id'
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'google_client_secret'

但是当我们为 android 创建一个谷歌控制台应用程序时,它不提供任何秘密,而只提供 client_Id。

我所做的是我刚刚从设置中删除了SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = 'google_client_secret',我的 api 仍然可以正常工作。它给出了没有任何错误的响应。

之后,我也尝试删除 SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = 'google_client_id',但我的 api 仍然运行良好。

所以我现在很困惑我们为什么要使用它们?为什么即使没有 client_id 和 secret,我的应用也能正常运行?

这就是我发送请求的方式;

http://127.0.0.1:8000/social/auth/token

请求正文;

{
    "grant_type": "password",
    "username": "myusername",
    "password": "mypassword",
    "client_id": "client id created in my backend when creating a new application for the social login (different from google client id)",
    "client_secret": "client secret created in my backend when creating a new application for the social login (different from google client secret)"
}

输出:

{
  "access_token": "some_token",
  "expires_in": 36000,
  "token_type": "Bearer",
  "scope": "read write",
  "refresh_token": "some_other_token"
}

另一个保存用户的请求;

http://127.0.0.1:8000/social/auth/convert_token

{
    "token": "token recieved from frontend from google",
    "backend": "google-oauth2",
    "grant_type": "convert_token",
    "client_id": "...",
    "client_secret": "..."
}

OUTPUT 与第一个请求相同,它还将详细信息添加到数据库中。

【问题讨论】:

  • 即使你要求一个新的访问令牌它仍然有效吗?
  • 是的,它有效。让我编辑我的问题以显示我如何发送请求。
  • 好的,您手动构建请求了吗?如果你没有使用SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,那么请求中的值是从哪里来的?
  • 正如我在问题中提到的,我正在使用 DRF Social oauth2,它在设置社交身份验证应用程序时提供这些凭据。
  • 首先我认为它可能只是在创建令牌,但我真的很惊讶它能够访问 google 帐户数据并将其存储在数据库中。

标签: django-rest-framework token google-login python-social-auth


【解决方案1】:

我希望你已经解决了这个问题。首先,drf-social-oauth2 中的端点在 auth/ 之前没有 social/。您可以在此处查看端点演示:https://github.com/wagnerdelima/drf-social-oauth2#testing-the-setup

所以你应该向 http:127.0.0.1:8000/auth/convert_token 开火。

我不知道为什么响应是肯定的,因为您删除了有效的客户端和秘密 ID。但是,您新生成的令牌不一定有效,因为它们不是使用有效凭据生成的,您明白吗?

P.S:我是drf-social-oauth2的主要维护者。

【讨论】:

  • 在 url 中包含社交并不是什么大问题。我在设置网址时做到了。所以当然,这不是一件错误的事情。您应该检查 django-urls 以及我们如何设置它们。
猜你喜欢
  • 2019-06-19
  • 2023-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-05
  • 2019-01-19
  • 2012-05-26
  • 1970-01-01
相关资源
最近更新 更多