Skip to content

网易云音乐新版WebAPI分析。

shengchaojie edited this page Aug 22, 2017 · 12 revisions

网易基本上全面启用了新版的API。深坑!

加密方式:

访问网址: http://music.163.com/weapi/xxx?csrf_token=xxx.

csrf为cookie中的csrf。不确定是否必要。

请求的方法基本上都是POST。 加密内容几本上为:

params=jIreCfXUS16Mh1s%2BmUuz1ndex8NSLnK6ozE3TLk71c9eMT3TsVQGU2nKcUbmMXRAOEHYKjKAe4lASsqaGncbDwk1QDhSCg8F1S0S%2BZAF3XiTA3sLQrLKt%2B7Kc0XW31eaVBml9Z%2B81pUaKFP%2BIRn%2B2rmmRnxBU%2BXxW%2BkL1Kd6cPM3U0Exhobjqp81jyakNZYF&encSecKey=d2a0f9732f5a92de8647db9edc3e2bb531b14208955435602901e7c123d77f6b52b6e40e6a084828dcd6f381bcebb3ea2e70e4c868f911792d2708f6975e61f18d33d4cf4f872acfaf068e1449b710b6b6f5097d91442f36d4588229da9ff8c0b1d2d3c933e86f843bc8a753b8e63f45860ea9c1c7185d4dd8dfa2dbcd25f96f

其中有两部分。 params 以及 seckey。 前者为对称加密后的参数。 后者为对称加密密钥。加密代码如下。参考这篇Blog

modulus = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
nonce = '0CoJUm6Qyw8W8jud'
pubKey = '010001'

def createSecretKey(size):
    return (''.join(map(lambda xx: (hex(ord(xx))[2:]), os.urandom(size))))[0:16]

def aesEncrypt(text, secKey):
    pad = 16 - len(text) % 16
    text = text + pad * chr(pad)
    encryptor = AES.new(secKey, 2, '0102030405060708')
    ciphertext = encryptor.encrypt(text)
    ciphertext = base64.b64encode(ciphertext)
    return ciphertext

def rsaEncrypt(text, pubKey, modulus):
    text = text[::-1]
    rs = int(text.encode('hex'), 16) ** int(pubKey, 16) % int(modulus, 16)
    return format(rs, 'x').zfill(256)

def encrypted_request(text):
    text = json.dumps(text)
    secKey = createSecretKey(16)
    encText = aesEncrypt(aesEncrypt(text, nonce), secKey)
    encSecKey = rsaEncrypt(secKey, pubKey, modulus)
    data = {
        'params': encText,
        'encSecKey': encSecKey
    }
    return data

获取用户歌单

URL:http://music.163.com/weapi/user/playlist?csrf_token=

内容如下。uid为用户id。其他参数未测试。

            req = {
                "offset": 0,
                "uid": uid,
                "limit": limit,
                "csrf_token": csrf
            }

获取歌单详情

URL:http://music.163.com/weapi/v3/playlist/detail?csrf_token=

内容如下:

            req = {
                "id": playlist_id,
                "offset": 0,
                "total": True,
                "limit": 1000,
                "n": 1000,
                "csrf_token": csrf
            }

这个api的返回结果和以前的不一样。包含的不是歌曲的所有信息,而且歌曲的结构也有所改变。同时可以获取网络云盘的曲子。

获取歌曲URL:

URL:http://music.163.com/weapi/song/enhance/player/url?csrf_token=

内容:

            req = {
                "ids": [song['song_id']],
                "br": music['br'],
                "csrf_token": csrf
            }

ids 为歌曲id的列表。br为数字,即为比特率。

返回的结果中包含url。可能会有url的过期时间,不过具体测试时间>1h,即不为返回参数中的expr。

当网易云因为版权问题下架曲子之后,该api返回404.

20170218 => http://www.jianshu.com/p/3269321e0df5

增加播放列表

http://music.163.com/weapi/playlist/create?csrf_token= 内容:

          req = {
               "name":"playlistname",
               "csrf_token": csrf
          }

给播放列表增加歌曲,支持多首歌曲

内容:

          req = {
               "trackIds":[songId1,songId2],
               "pid":"playlistId",
               "op":"add",
               "csrf_token": csrf
          }