【发布时间】:2021-09-07 00:40:50
【问题描述】:
我一直在尝试实现Twitch user authorization code flow。我正在努力获取从 django 后端生成的授权 url 并将其传递给我的 Vue 前端,以便用户可以打开用户授权页面。
Postman 完美地恢复了生成的 url,但是当我尝试在 Vue 中使用 Requests 做同样的事情时,它给了我一个完全不同的对象。
Object { _events: {…}, _eventsCount: 1, _maxListeners: undefined, uri: {…}, method: "POST", readable: true, writable: true, explicitMethod: true, _qs: {…}, _auth: {…}, … }
auth.js:37:12
这是 Postman 返回的内容
"{\"url\": \"https://id.twitch.tv/oauth2/authorize\", \"client_id\": \"xafkse7a6uyor8uisaet1ma36mrp9l\", \"response_type\": \"code\", \"grant_type\": \"token\", \"scope\": \"user_read\"}"
我不确定是什么原因造成的。下面是用于达到这一点的代码。
views.py
import json
import os
import requests
from django.http.response import JsonResponse
from rest_framework.decorators import api_view
from rest_framework import status
# get user access token
@api_view(['POST'])
def get_twitch_user_token_uri(request):
endpoint = "https://id.twitch.tv/oauth2/authorize"
token_url = {
'url': endpoint,
'client_id': settings['TWITCH_CLIENT_ID'],
'response_type': 'code',
'grant_type': 'token',
'scope': 'user_read'
}
return JsonResponse(token_url , status=status.HTTP_200_OK)
登录.vue
methods: {
login() {
const authenticating = true;
this.$store.dispatch("auth/get_twitch_user_token_uri", authenticating);
}
}
auth.js
const actions = {
get_twitch_user_token_uri({ commit, state }) {
commit(GET_TWITCH_TOKEN_URI);
console.log(this.endpoint);
return auth.twitch_token(this.endpoint)
.then(({ data }) => commit(GET_TWITCH_TOKEN, data))
.then(() => commit(GET_TWITCH_TOKEN_SUCCESS))
.catch(() => commit(GET_TWITCH_TOKEN_FAILURE));
},
}
const mutations = {
[GET_TWITCH_TOKEN_URI](state) {
const url = "http://localhost:8000/api/auth/login";
state.authenticating = true;
const r = request.post(url);
this.endpoint = r;
},
}
上面是从我的 django 后端请求 url 时开始发生错误的地方。我确定我忽略了一些我只是不知道为什么它会返回错误响应的东西。
感谢任何帮助!谢谢。
【问题讨论】:
标签: django vue.js django-rest-framework request