问题
I recently stumbled across some documentation for the unofficial Pandora API.
I decided to try this with Python 3.
After heading to the Authentication page I saw that I first had to verfiy that the service was available in my country so I did this.
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)
It prints out:
<Response [200]>
b'{"stat":"ok","result":{"isAllowed":true}}'
Right. So I'm allowed to use the partner service.
Next I saw that I had to get a Partner Login.
So I got the info it said I needed from the Partners page.
Note this is not my login info. This was partner info I was told to choose from in the documentation.
username = "android"
password = "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7"
deviceModel = "android-generic"
Next, the documentation says to send a post request to one of the following links as the base url:
- http://tuner.pandora.com/services/json/
- https://tuner.pandora.com/services/json/
- http://internal-tuner.pandora.com/services/json/
- https://internal-tuner.pandora.com/services/json/
Now to encode the url parameters and put them after the base url.
It says I should take the above username, password, deviceModel, the method I want to call (for partner login it says it is "auth.PartnerLogin", and the version (it says pass in the string "5") and url encode them.
So I set up the url params in urlencoded format and fire off a POST request:
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)
But when I do it prints this out and tells me there was an error:
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}'
Has anyone else used this Api before? Why am I getting an error? Am I missing something here? Apparently pithos uses this api, and it is loading music fine for me.
Can anybody point me in the right direction here please?
回答1:
Looks like you passing data as parameters and using incorrect url.
Proper curl request:
########## 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}}
I would suggest use urllib2 sample.
Here working sample for our case:
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)
Output:
('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"}}')
来源:https://stackoverflow.com/questions/47897996/pandora-api-auth-partnerlogin-giving-error