From e9fe4351b4f36209ede8699581a14b4715bfeaa8 Mon Sep 17 00:00:00 2001 From: binaryify Date: Sun, 17 May 2020 20:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E6=8E=A5=E5=8F=A3=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=86=85=E5=AE=B9=E5=A2=9E=E5=8A=A0cookie=E5=AD=97?= =?UTF-8?q?=E6=AE=B5,=E6=94=AF=E6=8C=81=E6=89=8B=E5=8A=A8=E4=BC=A0?= =?UTF-8?q?=E5=85=A5cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.MD | 3 +++ app.js | 2 +- docs/README.md | 27 ++++++++++++++++----------- module/login.js | 11 ++++++++++- module/login_cellphone.js | 15 +++++++++++++-- package.json | 2 +- util/request.js | 6 ++++-- 7 files changed, 48 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 3d44374..13c3c57 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,4 +1,7 @@ # 更新日志 +### 3.30.0 | 2020.05.17 +- 登录接口返回内容增加`cookie`字段,支持手动传入cookie + ### 3.29.1 | 2020.05.13 - 调整通知接口分页参数 [#761](https://github.com/Binaryify/NeteaseCloudMusicApi/issues/761) diff --git a/app.js b/app.js index 4e54c8d..66ac17e 100644 --- a/app.js +++ b/app.js @@ -66,7 +66,7 @@ fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => { let question = require(path.join(__dirname, 'module', file)) app.use(route, (req, res) => { - let query = Object.assign({}, req.query, req.body, {cookie: req.cookies}) + let query = Object.assign({}, {cookie: req.cookies}, req.query, req.body ) question(query, request) .then(answer => { console.log('[OK]', decodeURIComponent(req.originalUrl)) diff --git a/docs/README.md b/docs/README.md index 54fcb3b..623b223 100644 --- a/docs/README.md +++ b/docs/README.md @@ -293,7 +293,10 @@ $ sudo docker run -d -p 3000:3000 netease-music-api #### 1. 手机登录 -**必选参数 :** `phone`: 手机号码 `password`: 密码 +**必选参数 :** +`phone`: 手机号码 + +`password`: 密码 **接口地址 :** `/login/cellphone` @@ -303,22 +306,26 @@ $ sudo docker run -d -p 3000:3000 netease-music-api #### 2. 邮箱登录 -~~ 注意 : 此接口被网易和谐了 , 待修复 , 暂时使用手机登录 (2017.05.20)~~ +**必选参数 :** -> 更新 : 此接口已经可以正常使用(2018.07.03) +`email`: 163 网易邮箱 -**必选参数 :** `email`: 163 网易邮箱 `password`: 密码 **接口地址 :** `/login` **调用例子 :** `/login?email=xxx@163.com&password=yyy` -返回数据如下图 : -![登录](https://raw.githubusercontent.com/Binaryify/NeteaseCloudMusicApi/master/static/%E7%99%BB%E5%BD%95.png) - 完成登录后 , 会在浏览器保存一个 Cookies 用作登录凭证 , 大部分 API 都需要用到这个 -Cookies +Cookies,非跨域情况请求会自动带上 Cookies,跨域情况参考`调用前须知` + +v3.30.0后支持手动传入cookie,登录接口返回内容新增 `cookie` 字段,保存到本地后,get请求带上`?cookie=xxx` 或者 post请求body带上 `cookie` 即可,如:`/user/cloud?cookie=xxx` 或者 +``` +{ + ..., + cookie:"xxx" +} +``` #### 注意 @@ -921,9 +928,7 @@ tags: 歌单标签 说明 : 使用歌单详情接口后 , 能得到的音乐的 id, 但不能得到的音乐 url, 调用此接口 , 传入的音乐 id( 可多个 , 用逗号隔开 ), 可以获取对应的音乐的 url( 不需要登录 ) -> 注 : 部分用户反馈获取的 url 会 403,[hwaphon](https://github.com/hwaphon)找到的 -> 解决方案是当获取到音乐的 id 后,将 -> https://music.163.com/song/media/outer/url?id=id.mp3 以 src 赋予 Audio 即可播放 +> 注 : 部分用户反馈获取的 url 会 403,[hwaphon](https://github.com/hwaphon)找到的解决方案是当获取到音乐的 id 后,将 https://music.163.com/song/media/outer/url?id=id.mp3 以 src 赋予 Audio 即可播放 **必选参数 :** `id` : 音乐 id diff --git a/module/login.js b/module/login.js index b28c3b2..faee09f 100644 --- a/module/login.js +++ b/module/login.js @@ -9,7 +9,7 @@ module.exports = async (query, request) => { password: crypto.createHash('md5').update(query.password).digest('hex'), rememberLogin: 'true' } - const result = await request( + let result = await request( 'POST', `https://music.163.com/weapi/login`, data, { crypto: 'weapi', ua: 'pc', cookie: query.cookie, proxy: query.proxy } ) @@ -23,5 +23,14 @@ module.exports = async (query, request) => { } } } + if (result.body.code === 200) { + result = { + status: 200, + body: { + ...result.body, + cookie: result.cookie.join(';') + } + } + } return result } \ No newline at end of file diff --git a/module/login_cellphone.js b/module/login_cellphone.js index ed8f52d..7172f51 100644 --- a/module/login_cellphone.js +++ b/module/login_cellphone.js @@ -2,7 +2,7 @@ const crypto = require('crypto') -module.exports = (query, request) => { +module.exports = async (query, request) => { query.cookie.os = 'pc' const data = { phone: query.phone, @@ -10,8 +10,19 @@ module.exports = (query, request) => { password: crypto.createHash('md5').update(query.password).digest('hex'), rememberLogin: 'true' } - return request( + let result = await request( 'POST', `https://music.163.com/weapi/login/cellphone`, data, {crypto: 'weapi', ua: 'pc', cookie: query.cookie, proxy: query.proxy} ) + + if (result.body.code === 200) { + result = { + status: 200, + body: { + ...result.body, + cookie: result.cookie.join(';') + } + } + } + return result } diff --git a/package.json b/package.json index 76d9c6e..baadd05 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "NeteaseCloudMusicApi", - "version": "3.29.1", + "version": "3.30.0", "description": "网易云音乐 NodeJS 版 API", "scripts": { "start": "node app.js", diff --git a/util/request.js b/util/request.js index ac905ae..0b57d5b 100644 --- a/util/request.js +++ b/util/request.js @@ -40,7 +40,6 @@ const createRequest = (method, url, data, options) => { if (url.includes('music.163.com')) headers['Referer'] = 'https://music.163.com' // headers['X-Real-IP'] = '118.88.88.88' - if (typeof options.cookie === 'object') headers['Cookie'] = Object.keys(options.cookie) .map( @@ -51,7 +50,10 @@ const createRequest = (method, url, data, options) => { ) .join('; ') else if (options.cookie) headers['Cookie'] = options.cookie - + + if (!headers['Cookie']) { + headers['Cookie'] = options.token || '' + } if (options.crypto === 'weapi') { let csrfToken = (headers['Cookie'] || '').match(/_csrf=([^(;|$)]+)/) data.csrf_token = csrfToken ? csrfToken[1] : '' -- GitLab