1

我最近偶然发现了一些关于非官方 Pandora API 的文档。

我决定用 Python 3 试试这个。

在前往身份验证页面后,我看到我首先必须验证该服务在我的国家/地区是否可用,所以我这样做了。

import requests
import urllib

url = "http://internal-tuner.pandora.com/services/json/?method=test.checkLicensing"

res = requests.post(url)

print(res.status_code)
print(res.content)

它打印出来:

<Response [200]>
b'{"stat":"ok","result":{"isAllowed":true}}'

对。所以我被允许使用合作伙伴服务。

接下来我看到我必须获得一个Partner Login

所以我从合作伙伴页面得到了它说我需要的信息。

请注意,这不是我的登录信息。这是我被告知在文档中选择的合作伙伴信息。

username = "android"
password = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7"
deviceModel = "android-generic"

接下来,文档说将发布请求发送到以下链接之一作为基本url:

现在对 url 参数进行编码并将它们放在基本 url 之后。它说我应该采用上面的username, password, deviceModel,我想调用的方法(对于合作伙伴登录,它说它是“auth.PartnerLogin”,以及版本(它说传入字符串“5”)和 url 对它们进行编码。

所以我以 urlencoded 格式设置 url 参数并触发 POST 请求:

import requests
import urllib

url = "http://internal-tuner.pandora.com/services/json/?"

username = "android"
password = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7"
deviceModel = "android-generic"

data = {
    "method": "auth.partnerLogin",
    "username": username,
    "password": password,
    "deviceModel": deviceModel,
    "version": "5"
}

url += urllib.parse.urlencode(data)

res = requests.post(url)

print("url:", url)
print("response:", res)
print("content:", res.content)

但是当我这样做时,它会打印出来并告诉我有一个错误:

url: http://internal-tuner.pandora.com/services/json/?method=auth.partnerLogin&username=android&password=AC7IBG09A3DTSYM4R41UJWL07VLN8JI7&deviceModel=android-generic&version=5
response: <Response [200]>
content: b'{"stat":"fail","message":"An unexpected error occurred","code":9}'

有其他人以前使用过这个 Api 吗?为什么我会收到错误消息?我在这里错过了什么吗?显然pithos使用了这个api,它对我来说加载音乐很好。

有人可以在这里指出我正确的方向吗?

4

1 回答 1

1

看起来您将数据作为参数传递并使用不正确的 url。

正确的卷曲请求:

########## REQUEST ##########
curl  -i  --data '{ "username": "android", "password": "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7", "deviceModel": "android-generic", "version": "5", "includeUrls": true }' -X POST 'https://tuner.pandora.com:443/services/json/?method=auth.partnerLogin'   -H "Content-Type: application/json"  -A 'pinobar'
########## OUTPUT ##########
HTTP/1.1 200 OK
Date: Thu, 04 Jan 2018 03:46:54 GMT
Server: Apache
Content-Type: text/plain; charset=utf-8
Content-Length: 741
Cache-Control: must-revalidate, max-age=0
Expires: -1
Vary: Accept-Encoding

{"stat":"ok","result":{"syncTime":"f6f071bb4b886bc3545fbd66701b8d38","deviceProperties":{"followOnAdRefreshInterval":3,"ooyala":{"streamingPercentage":0,"streamingWhitelist":[534051315],"videoAdBufferRetryCount":3,"videoAdLoadingTimeout":2,"videoAdPlayTimeout":8},"videoAdUniqueInterval":0,"videoAdStartInterval":180,"optionalFeatures":{"optionalFeature":[{"feature":"useAudioProxy2","enabled":"false","platformVersionRange":{"low":"4.0","high":"5.0.0"},"productVersionRange":{"low":"1.6","high":"*"}}]},"adRefreshInterval":3,"videoAdRefreshInterval":870},"partnerAuthToken":"VADEjNzUq9Ew9HUkIzUT489kVe9kjo0If3","partnerId":"42","stationSkipUnit":"hour","urls":{"autoComplete":"http://autocomplete.pandora.com/search"},"stationSkipLimit":6}}

我建议使用 urllib2 示例

这里是我们案例的工作示例:

import json
import urllib2

username    = "android"
password    = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7"
deviceModel = "android-generic"
url         = "https://tuner.pandora.com:443/services/json/?method=auth.partnerLogin"

values = {
    "username"   : username,
    "password"   : password,
    "deviceModel": deviceModel,
    "version"    : "5"
}

data = json.dumps(values)
headers = {'content-type': 'application/json'}
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
content = response.read()

print("data:", data)
print("url:", url)
print("response:", response)
print("content:", content)

输出:

('url:', 'https://tuner.pandora.com:443/services/json/?method=auth.partnerLogin')
('response:', <addinfourl at 4509594832 whose fp = <socket._fileobject object at 0x10c7c0bd0>>)
('content:', '{"stat":"ok","result":{"stationSkipLimit":6,"partnerId":"42","partnerAuthToken":"VAEIniGnwSV1exsWHgUcsQgV5HA63B1nFA","syncTime":"4663310634ae885f45f489b2ab918a66","deviceProperties":{"followOnAdRefreshInterval":3,"ooyala":{"streamingPercentage":0,"streamingWhitelist":[534051315],"videoAdBufferRetryCount":3,"videoAdLoadingTimeout":2,"videoAdPlayTimeout":8},"videoAdUniqueInterval":0,"videoAdStartInterval":180,"optionalFeatures":{"optionalFeature":[{"feature":"useAudioProxy2","enabled":"false","platformVersionRange":{"low":"4.0","high":"5.0.0"},"productVersionRange":{"low":"1.6","high":"*"}}]},"adRefreshInterval":3,"videoAdRefreshInterval":870},"stationSkipUnit":"hour"}}')
于 2018-01-04T04:25:50.030 回答