我最近偶然发现了some documentation for the unofficial Pandora API.
我决定尝试使用Python 3。
在前往the Authentication page之后,我看到我首先必须确认该服务在我的国家/地区可用,所以我这样做了。
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}}'
右。所以我被允许使用合作伙伴服务。
接下来我看到我必须得到合作伙伴登录。
所以我从the Partners page获得了我所需要的信息。
username = "android"
password = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7"
deviceModel = "android-generic"
接下来,文档说要将帖子请求发送到以下链接之一作为 base 网址:
现在编码url参数并将它们放在基本URL之后。
它说我应该采用以上username
,password
,deviceModel
,我想要调用的方法(对于合作伙伴登录,它说它是“auth.PartnerLogin”)和版本(它说传递字符串“5”)并且url对它们进行编码。
所以我以urlencoded格式设置url params并触发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,它正在为我加载音乐。
有人能指出我正确的方向吗?
答案 0 :(得分:1)
看起来您将数据作为参数传递并使用了错误的网址。
正确的卷曲请求:
########## 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 sample。
这是我们案例的工作样本:
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"}}')