前端网易云项目462

网易云项目调取接口需要验证登录咋整啊
之前调用没出现过这情况,突然出现的

img

这个问题我前段时间也遇到过,如果您在调用网易云音乐的接口时需要进行登录验证的话可以看一下我的步骤:

  1. 获取登录凭证

首先,您需要使用用户名和密码登录到网易云音乐,并获取登录凭证。通常情况下,您可以在浏览器中登录网易云音乐,并使用浏览器的开发者工具查看登录请求的响应,从中获取登录凭证。登录凭证通常是一个包含用户登录信息的 JSON 对象,例如:

{
  "code": 200,
  "account": {
    "id": 123456,
    "userName": "example",
    "type": 1,
    "status": 0,
    "whitelistAuthority": 0,
    "createTime": 1551234567890,
    "salt": "ABCDEF123456",
    "tokenVersion": 1,
    "ban": 0,
    "baoyueVersion": 1,
    "donateVersion": 0,
    "vipType": 0,
    "viptypeVersion": -1,
    "anonimousUser": false
  },
  "profile": {
    "userId": 123456,
    "nickname": "example",
    "avatarUrl": "https://example.com/avatar.jpg",
    "backgroundUrl": "https://example.com/background.jpg",
    "signature": "Hello, World!",
    "createTime": 1551234567890,
    "userType": 0,
    "djStatus": 0,
    "gender": 1,
    "city": {
      "id": 440300,
      "name": "深圳市"
    },
    "province": {
      "id": 440000,
      "name": "广东省"
    },
    "mutual": false,
    "remarkName": null,
    "authStatus": 0,
    "expertTags": null,
    "experts": null,
    "vipRights": null,
    "avatarDetail": null,
    "anchor": false,
    "backgroundImgId": 109951165684888370,
    "backgroundImgIdStr": "109951165684888376",
    "avatarImgId": 109951165684888390,
    "avatarImgIdStr": "109951165684888388",
    "followeds": 0,
    "follows": 1,
    "eventCount": 0,
    "playlistCount": 3,
    "playlistBeSubscribedCount": 0
  },
  "bindings": [
    {
      "userId": 123456,
      "url": "",
      "type": 5,
      "tokenJsonStr": "{\"countrycode\":\"\",\"cellphone\":\"123456789\",\"hasPassword\":true}",
      "bindingTime": 1551234567890,
      "expired": false,
      "expiresIn": 2147483647,
      "refreshTime": 1551234567890,
      "id": 123456789012345678
    }
  ],
  "cookie": "MUSIC_U=ABCDEF123456; Expires=Mon, 11-Jul-2023 05:42:07 GMT; Path=/; Secure"
}

在上述示例中,cookie 字段包含了登录凭证,您可以将其保存在客户端的 Cookie 中,以便在后续的请求中使用。

  1. 在请求头中添加登录凭证

在调用网易云音乐的接口时,您需要在请求头中添加登录凭证。通常情况下,您可以使用 axios 等 HTTP 客户端库发送请求,并在请求头中添加 Cookie 字段,例如:

const axios = require('axios');

const cookie = 'MUSIC_U=ABCDEF123456; Expires=Mon, 11-Jul-2023 05:42:07 GMT; Path=/; Secure';

axios.get('https://music.163.com/api/playlist/detail?id=123456', {
  headers: {
    Cookie: cookie,
  },
}).then(response => {
  console.log(response.data);
}).catch(error => {
  console.error(error);
});

在上述代码中,我使用了 axios 库发送了一个 GET 请求,并在请求头中添加了 Cookie 字段,以便在请求中包含登录凭证。如果您使用其他 HTTP 客户端库发送请求,可能需要根据具体的库文档来添加请求头。