我正在为Android开发一个Pandora应用程序,这样我就可以添加一个Wear应用程序,当我试图以用户身份在我的应用程序的服务中连接时,我得到了一个错误代码13:
boolean partnerLoggedIn = Pandora.partnerLogin();
if (partnerLoggedIn) {
boolean userLoggedIn = Pandora.userLogin(username, password);
if (userLoggedIn) {
//Do post login stuff下面是partnerLogin()。我成功地解析了响应中的所有数据,没有任何问题:
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;
}下面是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;
}下面是我与潘多拉交流的AsyncTask:
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();
}现在,unoffical Pandora JSON api的错误13是"INSUFFICIENT_CONNECTIVITY。错误的同步时间?“
根据api文档,我的同步时间被计算出来了:
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;
}这一切在大约一周前就可以正常工作了,而且我还没有更改getSyncTime函数。我尝试将所有内容恢复到正常工作状态,但仍然收到此错误。已尝试在WiFi和4G上使用手机(以防万一)。我使用的凭据是正确的,因为错误的凭据将是不同的错误代码。
测试似乎表明syncTime在解密时丢失了几个数字(或者根本就没有得到它们):
例如,partnerLogin响应具有
"syncTime":"3fdb87fd2ca86037a263ab0ba76f77dc"它被存储为字符串。通过decrypt()运行它会产生以下结果:
1435335432它不应该是像"1435335432753“这样的东西,有13位数字,而不是10位,是服务器时间戳之类的吗?下面是解密:
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;
}
}这似乎工作正常,因为前10个数字是正确的。
发布于 2015-06-27 03:17:22
叹一口气。显然,我需要使用秒,而不是毫秒。所有内容都必须是10位数。
https://stackoverflow.com/questions/31077429
复制相似问题