通過豆瓣api可以獲取很多電影、書籍等的數(shù)據(jù)信息。但是用微信小程序請(qǐng)求豆瓣api時(shí),竟然被豆瓣拒絕了。(豆瓣設(shè)置了小程序的訪問權(quán)限)。
問題
小程序請(qǐng)求是這樣子:
onLoad: function (options) {
this.getMoviesData('https://api.douban.com/v2/book/1220562')
},
getMoviesData:function(url){
wx.request({
url: url,
data: {},
method: 'GET',
header: { 'content-type': 'application/json' },
success: function (res) {
console.log(res)
},
fail: function () {
console.log('fail')
},
})
}
錯(cuò)誤這樣子

解決
1、使用Nginx
- 首先下載Nginx
- 解壓
- 打開解壓文件nginx-1.13.12(這是你的解壓文件名)/conf/nginx.conf
- 在文件中找到server {},在server {}下添加
location /v2/ {
proxy_store off;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Referer 'no-referrer-when-downgrade';
proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36';
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_pass https://api.douban.com/v2/;
}
重點(diǎn)是更改
proxy_set_header Referer 'no-referrer-when-downgrade';
proxy_set_header User-Agent 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36';
以此來代替小程序去請(qǐng)求豆瓣,然后把數(shù)據(jù)返回給小程序。
更改配置后保存,在nginx.exe文件夾下打開命令窗口,輸入start nginx,啟動(dòng)后每次修改配置,使用nginx -s reload重啟
常用的指令:
start nginx : 啟動(dòng)nginx
nginx -s reload :修改配置后重新加載生效
nginx -s reopen :重新打開日志文件
nginx -t -c /path/to/nginx.conf 測(cè)試nginx配置文件是否正確
關(guān)閉nginx:
nginx -s stop :快速停止nginx
nginx -s quit :完整有序的停止nginx
注意,我是在windows下進(jìn)行開發(fā)和配置
如何使用:只需把請(qǐng)求的url的協(xié)議和域名替換成http://localhost/v2/,例如https://api.douban.com/v2/book/1220562=》http://localhost/v2/book/1220562
測(cè)試
onLoad: function (options) {
this.getMoviesData('http://localhost/v2/book/1220562')
},
getMoviesData:function(url){
wx.request({
url: url,
data: {},
method: 'GET',
header: { 'content-type': 'application/json' },
success: function (res) {
console.log(res)
},
fail: function () {
console.log('fail')
},
})
}
竟然還是錯(cuò)誤?。?!

狀態(tài)碼4xx客戶端錯(cuò)誤,400Bad Request 意思是我們發(fā)送了一個(gè)錯(cuò)誤的請(qǐng)求。經(jīng)過嘗試發(fā)現(xiàn),把header請(qǐng)求改成 header: { 'content-type': 'application/xml' }就可以了。額。。。明明獲取的數(shù)據(jù)就是json,。。??赡苁切〕绦蚝笈_(tái)對(duì)header做了限制。
終于等到你(正確測(cè)試)
onLoad: function (options) {
this.getMoviesData('http://localhost/v2/book/1220562')
},
getMoviesData:function(url){
wx.request({
url: url,
data: {},
method: 'GET',
header: { 'content-type': 'application/xml' },
success: function (res) {
console.log(res)
},
fail: function () {
console.log('fail')
},
})
}

2、使用node自己搭建代理
搭建本地服務(wù)器,監(jiān)聽8000端口,在服務(wù)器內(nèi)部調(diào)用http.get請(qǐng)求豆瓣數(shù)據(jù),將獲取到的數(shù)據(jù)返回給瀏覽器。
node.js代碼
var url=require('url');
var http = require('http');
var server = http.createServer((request, response) => {
//獲取要訪問的路徑
var pathname=url.parse(request.url).pathname;
//生成整個(gè)訪問鏈接
var requrl='http://api.douban.com'+pathname;
//緩存數(shù)據(jù)
var body = [];
response.writeHead(200, { 'Content-Type': 'application/json' });
//發(fā)起代理請(qǐng)求
http.get(requrl, function (res) {
res.on('data', (chunk) => {
body.push(chunk)
});
res.on('end', () => {
body = Buffer.concat(body);
//請(qǐng)求回來的數(shù)據(jù)寫入瀏覽器
response.end(body.toString())
})
});
})
server.listen(8000)
微信小程序發(fā)起請(qǐng)求
wx.request({
url: 'http://localhost:8000/v2/movie/in_theaters',
// url: 'http://localhost:8000',
method: 'GET',
header: { 'content-type': 'application/xml' },
success: function (res) {
console.log(res)
}
})
打印數(shù)據(jù)輸出到微信開發(fā)者工具的控制臺(tái):

微信請(qǐng)求本地端口,本地端口的數(shù)據(jù)是從豆瓣獲取的,代理相當(dāng)于起了'中間商'的作用。缺點(diǎn)是,只能本地獲取,外界無法獲取。除非把node放在真正的服務(wù)器上。
3、使用https://douban.uieee.com
https://douban.uieee.com是某大佬搭建的代理,
https://api.douban.com/v2/book/1220562=》https://douban.uieee.com/v2/book/1220562