How can I resolve a code 13 from Pandora on Android?

371 Views Asked by At

I am working on a Pandora app for Android so I can add a Wear app to it, and am getting an Error code 13 when I try to connect as a user in my app's Service:

boolean partnerLoggedIn = Pandora.partnerLogin();
if (partnerLoggedIn) {
    boolean userLoggedIn = Pandora.userLogin(username, password);
    if (userLoggedIn) {
       //Do post login stuff

Here is partnerLogin(). I get a success and am able to parse all the data from the response without issue:

public static boolean partnerLogin() throws JSONException {
    final JSONObject body = Partner.toJSON();
    final PandoraRequest request = new PandoraRequest("auth.partnerLogin", body, handler);
    Partner.setRequestSyncTime(System.currentTimeMillis());
    request.execute(JSON_URL + "auth.partnerLogin");
    String result = null;

    try {
        result = request.get();
    } catch (final InterruptedException e) {
        Log.w(TAG, "Connection interrupted for partnerLogin");
    } catch (final ExecutionException e) {
        Log.w(TAG, "Failed to execute partnerLogin task");
    }
    return Partner.parseLoginResponse(result);
}
public static JSONObject toJSON() {
    final JSONObject body = new JSONObject();
    try {
        body.put("username", "android");
        body.put("password", "AC7IBG09A3DTSYM4R41UJWL07VLN8JI7");
        body.put("deviceModel", "android-generic");
        body.put("version", Pandora.PROTOCOL_VERSION);
    }  catch (JSONException e) {
        Log.e(TAG, "toJSON failed: " + e.getMessage());
    }
    return body;
}

Here's userLogin():

    public static boolean userLogin(final String username, final String password) {
    if (Partner.getPartnerAuthToken() != null) {
        User.setUsername(username);
        User.setPassword(password);

        final JSONObject body = User.toJSON();

        try {
            body.put("syncTime", Partner.getSyncTime());
            body.put("partnerAuthToken", Partner.getPartnerAuthToken());

            final PandoraRequest request = new PandoraRequest("auth.userLogin", body, true, handler);
            final String auth = URLEncoder.encode(Partner.getPartnerAuthToken(), "utf-8");
            final String loginURLMethod = String.format("&auth_token=%s&partner_id=%s", auth, Partner.getPartnerId());
            request.execute(JSON_URL + "auth.userLogin" + loginURLMethod);

            try {
                final String result = request.get();
                return User.parseLoginResponse(result);
            } catch (final InterruptedException e) {
                Log.w(TAG, "Connection interrupted for userLogin");
            } catch (final ExecutionException e) {
                Log.w(TAG, "Failed to execute userLogin task");
            }
        } catch (final UnsupportedEncodingException e) {
            Log.e(TAG, "Failed to encode partner auth token: " + Partner.getPartnerAuthToken());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return false;
}

And here is my AsyncTask to communicate with Pandora:

protected String doInBackground(String... pandoraURL) {
    final StringBuilder result = new StringBuilder();

    for (final String element : pandoraURL) {
        try {
            //Log.e(TAG, "URL: " + element);
            final URL url = new URL(element);
            final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            final OutputStream os = urlConnection.getOutputStream();
            if(encrypted) {
                final Encryption encryption = new Encryption();
                final String encrypted = encryption.encrypt(json.toString());
                os.write(encrypted.getBytes());
            } else {
                os.write(json.toString().getBytes());
            }
            os.flush();

            Log.i(TAG, "Response Code: " + urlConnection.getResponseCode());

            if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                final BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    result.append(line);
                }
                br.close();
            }
            os.close();
            urlConnection.disconnect();
        } catch (MalformedURLException e) {
            Log.e(TAG, "Bad URL: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "Could not connect: " + e.getMessage());
        }
    }

    if(result.toString().contains("\"stat\":\"fail\"")) {
        parseFailureMessage(result.toString());
    }

    return result.toString();
}

Now, the unoffical Pandora JSON api has error 13 as being "INSUFFICIENT_CONNECTIVITY. Bad sync time?"

I have my sync time being caclualted thusly, as per the api documentation:

public static long getSyncTime() {
    Encryption crypt = new Encryption();
    String decryptedSyncTime = crypt.decrypt(syncTime);
    final long time = System.currentTimeMillis() + Partner.getRequestSyncTime() - Long.valueOf(decryptedSyncTime);
    return time;
}

This was all working about a week ago, and I haven't changed the getSyncTime function. I tried reverting everything back to when it WAS working, but I am still getting this error. Tried with the phone on WiFi and 4G (just in case.) The credentials I am using are correct, as wrong ones would be a different error code.

Tests seem to point to the syncTime dropping a few digits when it's decrypted (or not getting them in the first place):

The partnerLogin response, for example, has

"syncTime":"3fdb87fd2ca86037a263ab0ba76f77dc"

Which is stored as a String. running it through decrypt() yields:

1435335432

Shouldn't it be something like "1435335432753", having 13 digits, not 10, being a server timestamp and all? Here is decrypt:

    public String decrypt(final String encrypted) {
    try {
        final Cipher blowfishECB = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        final SecretKeySpec blowfishKey = new SecretKeySpec(DECRYPT_PASSWORD.getBytes("UTF8"), "Blowfish");
        blowfishECB.init(Cipher.DECRYPT_MODE, blowfishKey);
        final byte[] decryptedBytes = blowfishECB.doFinal(decodeHex(encrypted.toCharArray()));

        // First 4 bytes are garbage according to specification (deletes first 4 bytes)
        final byte[] trimGarbage = Arrays.copyOfRange(decryptedBytes, 4, decryptedBytes.length);
        return new String(trimGarbage);
    } catch (final Exception e) {
        Log.e(TAG, "Failed to decrypt content", e);
        return null;
    }
}

Which seems to work OK as the first 10 digits are correct.

1

There are 1 best solutions below

0
Merkidemis On

Sigh. Apparently I need to use seconds and not milliseconds. Everything needs to be 10 digits.