【问题标题】:Requests in Vuejs returning different response than Postman from django rest-framework backendVuejs 中的请求返回的响应与来自 d​​jango rest-framework 后端的 Postman 不同
【发布时间】: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


    【解决方案1】:

    与我调用请求并将数据从动作传递到突变的方式有关。

    登录.vue

    methods: {
        login() {
          this.$store.dispatch("auth/get_twitch_user_token_uri");
          // .then(() => this.$router.push("/"));
        }
      }
    

    auth.js

    const actions = {
      async get_twitch_user_token_uri({ commit }) {
        commit(GET_TWITCH_TOKEN_URI);
        const options = {
          method: 'POST',
          header: { 'Content-Type': 'application/json' },
        };
        try {
          const response = await fetch("http://localhost:8000/api/auth/login", options);
          const data = await response.json();
          console.log(data);
          commit(SET_TWITCH_TOKEN_URI, data);
          return commit(GET_TWITCH_TOKEN_URI_SUCCESS);
        } catch (e) {
          return commit(GET_TWITCH_TOKEN_URI_FAILURE);
        }
      },
    }
    
    const mutations = {
      [GET_TWITCH_TOKEN_URI](state) {
        state.authenticating = true;
      },
      [SET_TWITCH_TOKEN_URI](state, data) {
        state.endpoint = data;
      },
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-14
      • 2014-03-24
      • 2020-07-07
      • 2018-03-18
      • 2020-09-01
      相关资源
      最近更新 更多