OAuth 2.0在Django Port 8000发射后,如何继续在React Port 3000上继续进行React Port 3000

发布于 2025-01-26 03:28:00 字数 1609 浏览 5 评论 0原文

我的应用程序由React Frontend和Django后端构建。

尽管React部分位于Django项目中的前端文件夹中,但我还是尝试使这两个部分都尽可能独立。

因此,基本上,Django仅作为API提供商,并且反应控制整个前端。

在开发模式下,Django在Localhost上运行,端口8000; React在端口3000上运行。 我已经在设置中完成了CORS设置。

CORS_ALLOWED_ORIGINS = [
    'http://localhost:3000'
]

因此,基本上,我可以独立地测试前端和后端。所有API提取在Django和React之间也可以正常工作。

但是,在我的应用程序中,我还需要连接以Spotify API做某事。 在身份验证过程中,我需要为Spotify身份验证回调提供重定向URL。

我在Spotify注册​​的URL是:

http://127.0.0.1:8000/api-spotify/callback

在我的urls.py中:

from .views import get_auth_callback

urlpatterns = [
    path('callback', get_auth_callback),

]

in View.py:

def get_auth_callback(request):
    code = request.GET.get('code')

    response = post('https://accounts.spotify.com/api/token', data={
        'code': code,
        'redirect_uri': env.str('REDIRECT_URL'),
        'grant_type': 'authorization_code',
        'client_id': env.str('CLIENT_ID'),
        'client_secret': env.str('CLIENT_SECRET')
    }).json()

    access_token = response.get('access_token')
    token_type = response.get('token_type')
    refresh_token = response.get('refresh_token')
    expires_in = response.get('expires_in')
    
    edit_user_token(request.session.session_key, refresh_token, access_token, expires_in, token_type)
    return redirect('/')

问题在最后一行:

  return redirect('/')

如果我从前端登录Spotify(端口3000),重定向链接将将我带到端口8000,因为重定向回调函数是在Django中设置的。

而且我在端口3000中的测试再也无法继续。

有没有解决此类问题的解决方案? 还是我需要始终仅在端口8000中进行NPM运行构建和测试?

My application is built by React frontend and Django backend.

Although the React part is in the frontend folder inside Django project, I try to make both parts as independent as they can.

So basically, Django only server as API provider, and React control the entire frontend.

In the dev mode, Django run on localhost with port 8000; React run on port 3000.
I have completed CORS setting in my setting.py

CORS_ALLOWED_ORIGINS = [
    'http://localhost:3000'
]

So basically, I can test both frontend and backend quite independently. All Api fetching also works properly between Django and React.

However, in my app, I also need to connect to Spotify API to do something.
During the authentication process, I need to provide Redirect url for Spotify authentication callback.

The url I register in Spotify is:

http://127.0.0.1:8000/api-spotify/callback

in my urls.py:

from .views import get_auth_callback

urlpatterns = [
    path('callback', get_auth_callback),

]

In view.py:

def get_auth_callback(request):
    code = request.GET.get('code')

    response = post('https://accounts.spotify.com/api/token', data={
        'code': code,
        'redirect_uri': env.str('REDIRECT_URL'),
        'grant_type': 'authorization_code',
        'client_id': env.str('CLIENT_ID'),
        'client_secret': env.str('CLIENT_SECRET')
    }).json()

    access_token = response.get('access_token')
    token_type = response.get('token_type')
    refresh_token = response.get('refresh_token')
    expires_in = response.get('expires_in')
    
    edit_user_token(request.session.session_key, refresh_token, access_token, expires_in, token_type)
    return redirect('/')

The problem is in the final line:

  return redirect('/')

If I login Spotify from my frontend (port 3000), the redirect link will bring me to the port 8000 because the redirect callback function is setup in Django.

And my testing in port 3000 can no longer continue.

Is there any solution for such issue?
Or I need to always do npm run build and test everything only in port 8000?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

安静被遗忘 2025-02-02 03:28:00

The problem is:
http://127.0.0.1:8000/api-spotify/callback

You have to register a new url in Spotify for the test port:
http://127.0.0.1:3000/api-spotify/callback

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文