python - Pandora Api auth.partnerLogin 给出错误

标签 python json api python-requests pandora

我最近偶然发现了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"

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

现在对 url 参数进行编码并将它们放在基本 url 后面。 它说我应该采用上面的用户名密码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,它可以很好地加载音乐。

有人能给我指出正确的方向吗?

最佳答案

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

正确的 curl 请求:

########## 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"}}')

关于python - Pandora Api auth.partnerLogin 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47897996/

相关文章:

asp.net-mvc - 从 IP 地址远程访问本地 ASP.NET Core 应用程序?

jquery - jQuery .load 与 getJSON 结果中的引号输出

python - 加权选择简短而简单

python - 两个时间实例之间的差异

python - 如何摆脱科学记数法并在 Python 中转换为 float ?

python - 遍历 JSON 以查找定义的对象是否存在

c - 使用 pulseaudio API 播放 wav 文件?

python - 将字符串转换为没有时间信息的日期对象

c# - 使用 DataContractJsonSerializer 将元素附加到文件中的 JSON 数组

javascript - 如何使用 Yesod 构建 AngularJS 应用程序