溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue + webpack繞過QQ音樂接口對host驗證的示例分析

發(fā)布時間:2021-02-08 10:50:14 來源:億速云 閱讀:190 作者:小新 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)vue + webpack繞過QQ音樂接口對host驗證的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

前言

最近在使用vue2.5+webpack3.6擼一個移動端音樂項目, 獲取全部歌單json數(shù)據(jù)時遇到了接口對host和referer的限制 ,故不能直接在前端使用jsonp對接口數(shù)據(jù)的讀取。 

一、 先實現(xiàn)使用jsonp讀取的方式安裝jsonp模塊并, 封裝請求方法

1. $ npm install -S jsonp

2. 封裝import originJSONP from 'jsonp'

function jsonp(url, data, options) {
 // 如果存在?則直接加params(data), 否則先加?再加 params(data)
 url += (url.indexOf('?') < 0 ? '?' : '') + params(data)
 // 結(jié)果返回一個Promise對象
 return new Promise((resolve, reject) => {
 originJSONP(url, options, (err, data) => {
  if (!err) {
  resolve(data)
  } else {
  reject(err)
  }
 })
 })
}

function params(data) {
 let params = ''
 for (var k in data) {
 let value = data[k] != undefined ? data[k] : ''
 // url += '&' + k + '=' + encodeURIComponent(value) ES5
 params += `&${k}=${encodeURIComponent(value)}` // ES6
 }
 // 去掉首個參數(shù)的&, 因為jsonp方法中參數(shù)自帶&
 return params ? params.substring(1) : ''
}

3. 請求數(shù)據(jù)

vue + webpack繞過QQ音樂接口對host驗證的示例分析

# 代碼
 const commonParams = {
 g_tk: 5381,
 inCharset: 'utf-8',
 outCharset: 'utf-8',
 notice: 0,
 format: 'jsonp'
}
const options = {
 param: 'jsonpCallback'
}

getRecommend() {
 const url = 'https://c.y.qq.com/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg'
 const data = Object.assign({}, commonParams, {
 platform: 'h6',
 uin: 0,
 needNewCode: 1
 })
 return jsonp(url, data, options)
}

4. 組件內(nèi)調(diào)用getRecommend()方法, 獲取數(shù)據(jù)

methods: {
 _getRecommend() {
 getRecommend().then((res) => {
  // ERR_OK = 0是自定義的語義化參數(shù), 對應(yīng)返回json對象的code
  if (res.code === ERR_OK) {
  console.log(res.data)
  const data = res.data
  this.slider = data.slider
  }
 })
 }
},
created() {
 this._getRecommend()
}

console.log(res.data)可打印出json數(shù)據(jù)

vue + webpack繞過QQ音樂接口對host驗證的示例分析

以上是使用jsonp的方式請求數(shù)據(jù), 但對于被host和referer限制的json, 需要繞過host驗證,先讓服務(wù)端請求接口,前端頁面再通過服務(wù)端請求數(shù)據(jù)。而不能像jsonp那樣直接前端請求json對象。報錯如下

vue + webpack繞過QQ音樂接口對host驗證的示例分析

二、后端axios(ajax)請求接口數(shù)據(jù)

1.  定義后端代理請求 build/webpack.dev.config.js

const axios = require('axios')
devServer: {
 before(app) {
 app.get('/api/getDiscList', function (req, res) {
  var url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
  axios.get(url, {
  headers: {
   referer: 'https://c.y.qq.com/',
   host: 'c.y.qq.com'
  },
  params: req.query
  }).then((response) => {
  res.json(response.data)
  }).catch((e) => {
  console.log(e)
  })
 })
 },
 # ...其他原來的代碼
}

2.  前端請求后端已獲取的遠(yuǎn)程數(shù)據(jù)

import axios from 'axios'function getDiscList() {
 // const url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
 const url = '/api/getDiscList'
 const data = Object.assign({}, commonParams, {
 // 以下參數(shù)自行參考源json文件的Query String Parameters
 platform: 'yqq',
 uin: 0,
 needNewCode: 0,
 hostUin: 0,
 sin: 0,
 ein: 29,
 sortId: 5,
 rnd: Math.random(),
 categoryId: 10000000,
 format: 'json'
 })
 return axios.get(url, {
 params: data
 }).then((res) => {
 return Promise.resolve(res.data)
 })
}

vue + webpack繞過QQ音樂接口對host驗證的示例分析

3. 組件內(nèi)調(diào)用getDiscList()方法, 可輸出json數(shù)據(jù)

methods: {
 _getRecommend() {
 getRecommend().then((res) => {
  if (res.code === ERR_OK) {
  // console.log(res.data)
  const data = res.data
  this.slider = data.slider
  }
 })
 },
 _getDiscList() {
 getDiscList().then((res) => {
  console.log(res.data)
 })
 }
},
created() {
 this._getRecommend()
 this._getDiscList()
}

總結(jié), vue+webpack項目中,如需請求獲取遠(yuǎn)程json數(shù)據(jù)時, 一般由兩種情況:

1. 未受host和referer限制的前端組件可以直接使用jsonp插件請求json對象

2. 受host和referer限制需要驗證的, 通過后端代理方式,使用axios請求, 前端再請求后端json對象

關(guān)于“vue + webpack繞過QQ音樂接口對host驗證的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI