| 1 |
package com.akdeniz.googleplaycrawler; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.InputStream; |
| 5 |
import java.math.BigInteger; |
| 6 |
import java.security.Key; |
| 7 |
import java.security.KeyFactory; |
| 8 |
import java.security.MessageDigest; |
| 9 |
import java.security.PublicKey; |
| 10 |
import java.security.spec.RSAPublicKeySpec; |
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
import java.util.Map; |
| 14 |
import java.util.PropertyResourceBundle; |
| 15 |
import java.util.ResourceBundle; |
| 16 |
|
| 17 |
import javax.crypto.Cipher; |
| 18 |
|
| 19 |
import org.apache.http.HttpEntity; |
| 20 |
import org.apache.http.HttpResponse; |
| 21 |
import org.apache.http.NameValuePair; |
| 22 |
import org.apache.http.client.ClientProtocolException; |
| 23 |
import org.apache.http.client.HttpClient; |
| 24 |
import org.apache.http.client.entity.UrlEncodedFormEntity; |
| 25 |
import org.apache.http.client.methods.HttpGet; |
| 26 |
import org.apache.http.client.methods.HttpPost; |
| 27 |
import org.apache.http.client.methods.HttpUriRequest; |
| 28 |
import org.apache.http.client.utils.URLEncodedUtils; |
| 29 |
import org.apache.http.conn.ClientConnectionManager; |
| 30 |
import org.apache.http.entity.ByteArrayEntity; |
| 31 |
import org.apache.http.impl.client.DefaultHttpClient; |
| 32 |
import org.apache.http.impl.conn.PoolingClientConnectionManager; |
| 33 |
import org.apache.http.impl.conn.SchemeRegistryFactory; |
| 34 |
import org.apache.http.message.BasicNameValuePair; |
| 35 |
|
| 36 |
import com.akdeniz.googleplaycrawler.GooglePlay.AndroidAppDeliveryData; |
| 37 |
import com.akdeniz.googleplaycrawler.GooglePlay.AndroidCheckinRequest; |
| 38 |
import com.akdeniz.googleplaycrawler.GooglePlay.AndroidCheckinResponse; |
| 39 |
import com.akdeniz.googleplaycrawler.GooglePlay.BrowseResponse; |
| 40 |
import com.akdeniz.googleplaycrawler.GooglePlay.BulkDetailsRequest; |
| 41 |
import com.akdeniz.googleplaycrawler.GooglePlay.BulkDetailsRequest.Builder; |
| 42 |
import com.akdeniz.googleplaycrawler.GooglePlay.BulkDetailsResponse; |
| 43 |
import com.akdeniz.googleplaycrawler.GooglePlay.BuyResponse; |
| 44 |
import com.akdeniz.googleplaycrawler.GooglePlay.DetailsResponse; |
| 45 |
import com.akdeniz.googleplaycrawler.GooglePlay.ListResponse; |
| 46 |
import com.akdeniz.googleplaycrawler.GooglePlay.ResponseWrapper; |
| 47 |
import com.akdeniz.googleplaycrawler.GooglePlay.ReviewResponse; |
| 48 |
import com.akdeniz.googleplaycrawler.GooglePlay.SearchResponse; |
| 49 |
import com.akdeniz.googleplaycrawler.GooglePlay.UploadDeviceConfigRequest; |
| 50 |
import com.akdeniz.googleplaycrawler.GooglePlay.UploadDeviceConfigResponse; |
| 51 |
import com.akdeniz.googleplaycrawler.misc.Base64; |
| 52 |
|
| 53 |
/** |
| 54 |
* This class provides |
| 55 |
* <code>checkin, search, details, bulkDetails, browse, list and download</code> |
| 56 |
* capabilities. It uses <code>Apache Commons HttpClient</code> for POST and GET |
| 57 |
* requests. |
| 58 |
* |
| 59 |
* <p> |
| 60 |
* <b>XXX : DO NOT call checkin, login and download consecutively. To allow |
| 61 |
* server to catch up, sleep for a while before download! (5 sec will do!) Also |
| 62 |
* it is recommended to call checkin once and use generated android-id for |
| 63 |
* further operations.</b> |
| 64 |
* </p> |
| 65 |
* |
| 66 |
* @author akdeniz |
| 67 |
* |
| 68 |
*/ |
| 69 |
public class GooglePlayAPI { |
| 70 |
|
| 71 |
private static final String CHECKIN_URL = "https://android.clients.google.com/checkin"; |
| 72 |
private static final String URL_LOGIN = "https://android.clients.google.com/auth"; |
| 73 |
private static final String C2DM_REGISTER_URL = "https://android.clients.google.com/c2dm/register2"; |
| 74 |
private static final String FDFE_URL = "https://android.clients.google.com/fdfe/"; |
| 75 |
private static final String LIST_URL = FDFE_URL + "list"; |
| 76 |
private static final String BROWSE_URL = FDFE_URL + "browse"; |
| 77 |
private static final String DETAILS_URL = FDFE_URL + "details"; |
| 78 |
private static final String SEARCH_URL = FDFE_URL + "search"; |
| 79 |
private static final String BULKDETAILS_URL = FDFE_URL + "bulkDetails"; |
| 80 |
private static final String PURCHASE_URL = FDFE_URL + "purchase"; |
| 81 |
private static final String REVIEWS_URL = FDFE_URL + "rev"; |
| 82 |
public static final String UPLOADDEVICECONFIG_URL = FDFE_URL + "uploadDeviceConfig"; |
| 83 |
private static final String RECOMMENDATIONS_URL = FDFE_URL + "rec"; |
| 84 |
private static final String DELIVERY_URL = FDFE_URL + "delivery"; |
| 85 |
|
| 86 |
private static final String ACCOUNT_TYPE_HOSTED_OR_GOOGLE = "HOSTED_OR_GOOGLE"; |
| 87 |
|
| 88 |
public static enum REVIEW_SORT { |
| 89 |
NEWEST(0), HIGHRATING(1), HELPFUL(2); |
| 90 |
|
| 91 |
public int value; |
| 92 |
|
| 93 |
private REVIEW_SORT(int value) { |
| 94 |
this.value = value; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public static enum RECOMMENDATION_TYPE { |
| 99 |
ALSO_VIEWED(1), ALSO_INSTALLED(2); |
| 100 |
|
| 101 |
public int value; |
| 102 |
|
| 103 |
private RECOMMENDATION_TYPE(int value) { |
| 104 |
this.value = value; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private String token; |
| 109 |
private String androidID; |
| 110 |
private String email; |
| 111 |
private String password; |
| 112 |
private HttpClient client; |
| 113 |
private String securityToken; |
| 114 |
private String localization; |
| 115 |
private String useragent; |
| 116 |
|
| 117 |
/** |
| 118 |
* Default constructor. ANDROID ID and Authentication token must be supplied |
| 119 |
* before any other operation. |
| 120 |
*/ |
| 121 |
public GooglePlayAPI() { |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Constructs a ready to login {@link GooglePlayAPI}. |
| 126 |
*/ |
| 127 |
public GooglePlayAPI(String email, String password, String androidID) { |
| 128 |
this(email, password); |
| 129 |
this.setAndroidID(androidID); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* If this constructor is used, Android ID must be generated by calling |
| 134 |
* <code>checkin()</code> or set by using <code>setAndroidID</code> before |
| 135 |
* using other abilities. |
| 136 |
*/ |
| 137 |
public GooglePlayAPI(String email, String password) { |
| 138 |
this.setEmail(email); |
| 139 |
this.password = password; |
| 140 |
setClient(new DefaultHttpClient(getConnectionManager())); |
| 141 |
// setUseragent("Android-Finsky/3.10.14 (api=3,versionCode=8016014,sdk=15,device=GT-I9300,hardware=aries,product=GT-I9300)"); |
| 142 |
setUseragent("Android-Finsky/6.5.08.D-all (versionCode=80650800,sdk=23,device=noblelte,hardware=noblelte,product=noblelte,build=MMB29K:user)"); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Connection manager to allow concurrent connections. |
| 147 |
* |
| 148 |
* @return {@link ClientConnectionManager} instance |
| 149 |
*/ |
| 150 |
public static ClientConnectionManager getConnectionManager() { |
| 151 |
PoolingClientConnectionManager connManager = new PoolingClientConnectionManager( |
| 152 |
SchemeRegistryFactory.createDefault()); |
| 153 |
connManager.setMaxTotal(100); |
| 154 |
connManager.setDefaultMaxPerRoute(30); |
| 155 |
return connManager; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Performs authentication on "ac2dm" service and match up android id, |
| 160 |
* security token and email by checking them in on this server. |
| 161 |
* |
| 162 |
* This function sets check-inded android ID and that can be taken either by |
| 163 |
* using <code>getToken()</code> or from returned |
| 164 |
* {@link AndroidCheckinResponse} instance. |
| 165 |
* |
| 166 |
*/ |
| 167 |
public AndroidCheckinResponse checkin() throws Exception { |
| 168 |
|
| 169 |
// this first checkin is for generating android-id |
| 170 |
AndroidCheckinResponse checkinResponse = postCheckin(Utils.generateAndroidCheckinRequest() |
| 171 |
.toByteArray()); |
| 172 |
this.setAndroidID(BigInteger.valueOf(checkinResponse.getAndroidId()).toString(16)); |
| 173 |
setSecurityToken((BigInteger.valueOf(checkinResponse.getSecurityToken()).toString(16))); |
| 174 |
|
| 175 |
String c2dmAuth = loginAC2DM(); |
| 176 |
//login(); |
| 177 |
//String c2dmAuth= getToken(); |
| 178 |
|
| 179 |
AndroidCheckinRequest.Builder checkInbuilder = AndroidCheckinRequest.newBuilder(Utils |
| 180 |
.generateAndroidCheckinRequest()); |
| 181 |
|
| 182 |
AndroidCheckinRequest build = checkInbuilder |
| 183 |
.setId(new BigInteger(this.getAndroidID(), 16).longValue()) |
| 184 |
.setSecurityToken(new BigInteger(getSecurityToken(), 16).longValue()) |
| 185 |
.addAccountCookie("[" + getEmail() + "]").addAccountCookie(c2dmAuth).build(); |
| 186 |
// this is the second checkin to match credentials with android-id |
| 187 |
return postCheckin(build.toByteArray()); |
| 188 |
} |
| 189 |
|
| 190 |
private static int readInt(byte[] bArr, int i) { |
| 191 |
return (((((bArr[i] & 255) << 24) | 0) | ((bArr[i + 1] & 255) << 16)) | ((bArr[i + 2] & 255) << 8)) |
| 192 |
| (bArr[i + 3] & 255); |
| 193 |
} |
| 194 |
public static PublicKey createKeyFromString(String str, byte[] bArr) { |
| 195 |
try { |
| 196 |
byte[] decode = Base64.decode(str, 0); |
| 197 |
int readInt = readInt(decode, 0); |
| 198 |
byte[] obj = new byte[readInt]; |
| 199 |
System.arraycopy(decode, 4, obj, 0, readInt); |
| 200 |
BigInteger bigInteger = new BigInteger(1, obj); |
| 201 |
int readInt2 = readInt(decode, readInt + 4); |
| 202 |
byte[] obj2 = new byte[readInt2]; |
| 203 |
System.arraycopy(decode, readInt + 8, obj2, 0, readInt2); |
| 204 |
BigInteger bigInteger2 = new BigInteger(1, obj2); |
| 205 |
decode = MessageDigest.getInstance("SHA-1").digest(decode); |
| 206 |
bArr[0] = (byte) 0; |
| 207 |
System.arraycopy(decode, 0, bArr, 1, 4); |
| 208 |
return KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(bigInteger, bigInteger2)); |
| 209 |
} catch (Throwable e) { |
| 210 |
throw new RuntimeException(e); |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
private static String encryptString(String str) { |
| 215 |
int i = 0; |
| 216 |
ResourceBundle bundle = PropertyResourceBundle.getBundle("com.akdeniz.googleplaycrawler.crypt"); |
| 217 |
String string=bundle.getString("key"); |
| 218 |
|
| 219 |
byte[] obj = new byte[5]; |
| 220 |
Key createKeyFromString = createKeyFromString(string, obj); |
| 221 |
if (createKeyFromString == null) { |
| 222 |
return null; |
| 223 |
} |
| 224 |
try { |
| 225 |
Cipher instance = Cipher |
| 226 |
.getInstance("RSA/ECB/OAEPWITHSHA1ANDMGF1PADDING"); |
| 227 |
byte[] bytes = str.getBytes("UTF-8"); |
| 228 |
int length = ((bytes.length - 1) / 86) + 1; |
| 229 |
byte[] obj2 = new byte[(length * 133)]; |
| 230 |
while (i < length) { |
| 231 |
instance.init(1, createKeyFromString); |
| 232 |
byte[] doFinal = instance.doFinal(bytes, i * 86, i == length |
| 233 |
+ -1 ? bytes.length - (i * 86) : 86); |
| 234 |
System.arraycopy(obj, 0, obj2, i * 133, obj.length); |
| 235 |
System.arraycopy(doFinal, 0, obj2, (i * 133) + obj.length, |
| 236 |
doFinal.length); |
| 237 |
i++; |
| 238 |
} |
| 239 |
return Base64.encodeToString(obj2, 10); |
| 240 |
} catch (Throwable e) { |
| 241 |
throw new RuntimeException(e); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Logins AC2DM server and returns authentication string. |
| 247 |
*/ |
| 248 |
public String loginAC2DM() throws IOException { |
| 249 |
HttpEntity c2dmResponseEntity = executePost(URL_LOGIN, |
| 250 |
new String[][] { |
| 251 |
{ "Email", this.getEmail() }, |
| 252 |
{ "EncryptedPasswd", encryptString(this.getEmail()+"\u0000"+this.password) }, |
| 253 |
{ "add_account", "1"}, |
| 254 |
{ "service", "ac2dm" }, |
| 255 |
{ "accountType", ACCOUNT_TYPE_HOSTED_OR_GOOGLE }, |
| 256 |
{ "has_permission", "1" }, |
| 257 |
{ "source", "android" }, |
| 258 |
{ "app", "com.google.android.gsf" }, |
| 259 |
{ "device_country", "us" }, |
| 260 |
{ "device_country", "us" }, |
| 261 |
{ "lang", "en" }, |
| 262 |
{ "sdk_version", "17" }, }, null); |
| 263 |
|
| 264 |
Map<String, String> c2dmAuth = Utils.parseResponse(new String(Utils.readAll(c2dmResponseEntity |
| 265 |
.getContent()))); |
| 266 |
return c2dmAuth.get("Auth"); |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
public Map<String, String> c2dmRegister(String application, String sender) throws IOException { |
| 271 |
|
| 272 |
String c2dmAuth = loginAC2DM(); |
| 273 |
String[][] data = new String[][] { |
| 274 |
{ "app", application }, |
| 275 |
{ "sender", sender }, |
| 276 |
{ "device", new BigInteger(this.getAndroidID(), 16).toString() } }; |
| 277 |
HttpEntity responseEntity = executePost(C2DM_REGISTER_URL, data, |
| 278 |
getHeaderParameters(c2dmAuth, null)); |
| 279 |
return Utils.parseResponse(new String(Utils.readAll(responseEntity.getContent()))); |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Equivalent of <code>setToken</code>. This function does not performs |
| 284 |
* authentication, it simply sets authentication token. |
| 285 |
*/ |
| 286 |
public void login(String token) throws Exception { |
| 287 |
setToken(token); |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Authenticates on server with given email and password and sets |
| 292 |
* authentication token. This token can be used to login instead of using |
| 293 |
* email and password every time. |
| 294 |
*/ |
| 295 |
public void login() throws Exception { |
| 296 |
|
| 297 |
HttpEntity responseEntity = executePost(URL_LOGIN, new String[][] { |
| 298 |
{ "Email", this.getEmail() }, |
| 299 |
{ "EncryptedPasswd", encryptString(this.getEmail()+"\u0000"+this.password) }, |
| 300 |
{ "service", "androidmarket" }, |
| 301 |
{ "add_account", "1"}, |
| 302 |
{ "accountType", ACCOUNT_TYPE_HOSTED_OR_GOOGLE }, |
| 303 |
{ "has_permission", "1" }, |
| 304 |
{ "source", "android" }, |
| 305 |
{ "androidId", this.getAndroidID() }, |
| 306 |
{ "app", "com.android.vending" }, |
| 307 |
{ "device_country", "en" }, |
| 308 |
{ "lang", "en" }, |
| 309 |
{ "sdk_version", "17" }, }, null); |
| 310 |
|
| 311 |
Map<String, String> response = Utils.parseResponse(new String(Utils.readAll(responseEntity |
| 312 |
.getContent()))); |
| 313 |
if (response.containsKey("Auth")) { |
| 314 |
setToken(response.get("Auth")); |
| 315 |
} |
| 316 |
else { |
| 317 |
throw new GooglePlayException("Authentication failed!"); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Equivalent of <code>search(query, null, null)</code> |
| 323 |
*/ |
| 324 |
public SearchResponse search(String query) throws IOException { |
| 325 |
return search(query, null, null); |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Fetches a search results for given query. Offset and numberOfResult |
| 330 |
* parameters are optional and <code>null</code> can be passed! |
| 331 |
*/ |
| 332 |
public SearchResponse search(String query, Integer offset, Integer numberOfResult) |
| 333 |
throws IOException { |
| 334 |
|
| 335 |
ResponseWrapper responseWrapper = executeGETRequest(SEARCH_URL, new String[][] { |
| 336 |
{ "c", "3" }, |
| 337 |
{ "q", query }, |
| 338 |
{ "o", (offset == null) ? null : String.valueOf(offset) }, |
| 339 |
{ "n", (numberOfResult == null) ? null : String.valueOf(numberOfResult) }, }); |
| 340 |
|
| 341 |
return responseWrapper.getPayload().getSearchResponse(); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Fetches detailed information about passed package name. If it is needed to |
| 346 |
* fetch information about more than one application, consider to use |
| 347 |
* <code>bulkDetails</code>. |
| 348 |
*/ |
| 349 |
public DetailsResponse details(String packageName) throws IOException { |
| 350 |
ResponseWrapper responseWrapper = executeGETRequest(DETAILS_URL, new String[][] { { |
| 351 |
"doc", |
| 352 |
packageName }, }); |
| 353 |
|
| 354 |
return responseWrapper.getPayload().getDetailsResponse(); |
| 355 |
} |
| 356 |
|
| 357 |
/** Equivalent of details but bulky one! */ |
| 358 |
public BulkDetailsResponse bulkDetails(List<String> packageNames) throws IOException { |
| 359 |
|
| 360 |
Builder bulkDetailsRequestBuilder = BulkDetailsRequest.newBuilder(); |
| 361 |
bulkDetailsRequestBuilder.addAllDocid(packageNames); |
| 362 |
|
| 363 |
ResponseWrapper responseWrapper = executePOSTRequest(BULKDETAILS_URL, bulkDetailsRequestBuilder |
| 364 |
.build().toByteArray(), "application/x-protobuf"); |
| 365 |
|
| 366 |
return responseWrapper.getPayload().getBulkDetailsResponse(); |
| 367 |
} |
| 368 |
|
| 369 |
/** Fetches available categories */ |
| 370 |
public BrowseResponse browse() throws IOException { |
| 371 |
|
| 372 |
return browse(null, null); |
| 373 |
} |
| 374 |
|
| 375 |
public BrowseResponse browse(String categoryId, String subCategoryId) throws IOException { |
| 376 |
|
| 377 |
ResponseWrapper responseWrapper = executeGETRequest(BROWSE_URL, new String[][] { |
| 378 |
{ "c", "3" }, |
| 379 |
{ "cat", categoryId }, |
| 380 |
{ "ctr", subCategoryId } }); |
| 381 |
|
| 382 |
return responseWrapper.getPayload().getBrowseResponse(); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Equivalent of <code>list(categoryId, null, null, null)</code>. It fetches |
| 387 |
* sub-categories of given category! |
| 388 |
*/ |
| 389 |
public ListResponse list(String categoryId) throws IOException { |
| 390 |
return list(categoryId, null, null, null); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Fetches applications within supplied category and sub-category. If |
| 395 |
* <code>null</code> is given for sub-category, it fetches sub-categories of |
| 396 |
* passed category. |
| 397 |
* |
| 398 |
* Default values for offset and numberOfResult are "0" and "20" respectively. |
| 399 |
* These values are determined by Google Play Store. |
| 400 |
*/ |
| 401 |
public ListResponse list(String categoryId, String subCategoryId, Integer offset, |
| 402 |
Integer numberOfResult) throws IOException { |
| 403 |
ResponseWrapper responseWrapper = executeGETRequest(LIST_URL, new String[][] { |
| 404 |
{ "c", "3" }, |
| 405 |
{ "cat", categoryId }, |
| 406 |
{ "ctr", subCategoryId }, |
| 407 |
{ "o", (offset == null) ? null : String.valueOf(offset) }, |
| 408 |
{ "n", (numberOfResult == null) ? null : String.valueOf(numberOfResult) }, }); |
| 409 |
|
| 410 |
return responseWrapper.getPayload().getListResponse(); |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Downloads given application package name, version and offer type. Version |
| 415 |
* code and offer type can be fetch by <code>details</code> interface. |
| 416 |
**/ |
| 417 |
public DownloadData download(String packageName, int versionCode, int offerType) |
| 418 |
throws IOException { |
| 419 |
|
| 420 |
BuyResponse buyResponse = purchase(packageName, versionCode, offerType); |
| 421 |
|
| 422 |
return new DownloadData(this, buyResponse.getPurchaseStatusResponse().getAppDeliveryData()); |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
public DownloadData delivery(String packageName, int versionCode, int offerType) |
| 427 |
throws IOException { |
| 428 |
ResponseWrapper responseWrapper = executeGETRequest(DELIVERY_URL, new String[][] { |
| 429 |
{ "ot", String.valueOf(offerType) }, |
| 430 |
{ "doc", packageName }, |
| 431 |
{ "vc", String.valueOf(versionCode) }, }); |
| 432 |
|
| 433 |
AndroidAppDeliveryData appDeliveryData = responseWrapper.getPayload().getDeliveryResponse() |
| 434 |
.getAppDeliveryData(); |
| 435 |
return new DownloadData(this, appDeliveryData); |
| 436 |
} |
| 437 |
|
| 438 |
/** |
| 439 |
* Posts given check-in request content and returns |
| 440 |
* {@link AndroidCheckinResponse}. |
| 441 |
*/ |
| 442 |
public AndroidCheckinResponse postCheckin(byte[] request) throws IOException { |
| 443 |
|
| 444 |
HttpEntity httpEntity = executePost(CHECKIN_URL, new ByteArrayEntity(request), new String[][] { |
| 445 |
{ "User-Agent", "Android-Checkin/2.0 (generic JRO03E); gzip" }, |
| 446 |
{ "Host", "android.clients.google.com" }, |
| 447 |
{ "Content-Type", "application/x-protobuffer" } }); |
| 448 |
return AndroidCheckinResponse.parseFrom(httpEntity.getContent()); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* This function is used for fetching download url and donwload cookie, rather |
| 453 |
* than actual purchasing. |
| 454 |
*/ |
| 455 |
private BuyResponse purchase(String packageName, int versionCode, int offerType) |
| 456 |
throws IOException { |
| 457 |
|
| 458 |
ResponseWrapper responseWrapper = executePOSTRequest(PURCHASE_URL, new String[][] { |
| 459 |
{ "ot", String.valueOf(offerType) }, |
| 460 |
{ "doc", packageName }, |
| 461 |
{ "vc", String.valueOf(versionCode) }, }); |
| 462 |
|
| 463 |
return responseWrapper.getPayload().getBuyResponse(); |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Fetches url content by executing GET request with provided cookie string. |
| 468 |
*/ |
| 469 |
public InputStream executeDownload(String url, String cookie) throws IOException { |
| 470 |
|
| 471 |
String[][] headerParams = new String[][] { |
| 472 |
{ "Cookie", cookie }, |
| 473 |
{ |
| 474 |
"User-Agent", |
| 475 |
"AndroidDownloadManager/4.1.1 (Linux; U; Android 4.1.1; Nexus S Build/JRO03E)" }, }; |
| 476 |
|
| 477 |
HttpEntity httpEntity = executeGet(url, null, headerParams); |
| 478 |
return httpEntity.getContent(); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Fetches the reviews of given package name by sorting passed choice. |
| 483 |
* |
| 484 |
* Default values for offset and numberOfResult are "0" and "20" respectively. |
| 485 |
* These values are determined by Google Play Store. |
| 486 |
*/ |
| 487 |
public ReviewResponse reviews(String packageName, REVIEW_SORT sort, Integer offset, |
| 488 |
Integer numberOfResult) throws IOException { |
| 489 |
ResponseWrapper responseWrapper = executeGETRequest(REVIEWS_URL, new String[][] { |
| 490 |
{ "doc", packageName }, |
| 491 |
{ "sort", (sort == null) ? null : String.valueOf(sort.value) }, |
| 492 |
{ "o", (offset == null) ? null : String.valueOf(offset) }, |
| 493 |
{ "n", (numberOfResult == null) ? null : String.valueOf(numberOfResult) } }); |
| 494 |
|
| 495 |
return responseWrapper.getPayload().getReviewResponse(); |
| 496 |
} |
| 497 |
|
| 498 |
/** |
| 499 |
* Uploads device configuration to google server so that can be seen from web |
| 500 |
* as a registered device!! |
| 501 |
* |
| 502 |
* @see https://play.google.com/store/account |
| 503 |
*/ |
| 504 |
public UploadDeviceConfigResponse uploadDeviceConfig() throws Exception { |
| 505 |
|
| 506 |
UploadDeviceConfigRequest request = UploadDeviceConfigRequest.newBuilder() |
| 507 |
.setDeviceConfiguration(Utils.getDeviceConfigurationProto()).build(); |
| 508 |
ResponseWrapper responseWrapper = executePOSTRequest(UPLOADDEVICECONFIG_URL, |
| 509 |
request.toByteArray(), "application/x-protobuf"); |
| 510 |
return responseWrapper.getPayload().getUploadDeviceConfigResponse(); |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Fetches the recommendations of given package name. |
| 515 |
* |
| 516 |
* Default values for offset and numberOfResult are "0" and "20" respectively. |
| 517 |
* These values are determined by Google Play Store. |
| 518 |
*/ |
| 519 |
public ListResponse recommendations(String packageName, RECOMMENDATION_TYPE type, Integer offset, |
| 520 |
Integer numberOfResult) throws IOException { |
| 521 |
ResponseWrapper responseWrapper = executeGETRequest(RECOMMENDATIONS_URL, new String[][] { |
| 522 |
{ "c", "3" }, |
| 523 |
{ "doc", packageName }, |
| 524 |
{ "rt", (type == null) ? null : String.valueOf(type.value) }, |
| 525 |
{ "o", (offset == null) ? null : String.valueOf(offset) }, |
| 526 |
{ "n", (numberOfResult == null) ? null : String.valueOf(numberOfResult) } }); |
| 527 |
|
| 528 |
return responseWrapper.getPayload().getListResponse(); |
| 529 |
} |
| 530 |
|
| 531 |
/* =======================Helper Functions====================== */ |
| 532 |
|
| 533 |
/** |
| 534 |
* Executes GET request and returns result as {@link ResponseWrapper}. |
| 535 |
* Standard header parameters will be used for request. |
| 536 |
* |
| 537 |
* @see getHeaderParameters |
| 538 |
* */ |
| 539 |
private ResponseWrapper executeGETRequest(String path, String[][] datapost) throws IOException { |
| 540 |
|
| 541 |
HttpEntity httpEntity = executeGet(path, datapost, getHeaderParameters(this.getToken(), null)); |
| 542 |
return GooglePlay.ResponseWrapper.parseFrom(httpEntity.getContent()); |
| 543 |
|
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Executes POST request and returns result as {@link ResponseWrapper}. |
| 548 |
* Standard header parameters will be used for request. |
| 549 |
* |
| 550 |
* @see getHeaderParameters |
| 551 |
* */ |
| 552 |
private ResponseWrapper executePOSTRequest(String path, String[][] datapost) throws IOException { |
| 553 |
|
| 554 |
HttpEntity httpEntity = executePost(path, datapost, getHeaderParameters(this.getToken(), null)); |
| 555 |
return GooglePlay.ResponseWrapper.parseFrom(httpEntity.getContent()); |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Executes POST request and returns result as {@link ResponseWrapper}. |
| 561 |
* Content type can be specified for given byte array. |
| 562 |
*/ |
| 563 |
public ResponseWrapper executePOSTRequest(String url, byte[] datapost, String contentType) |
| 564 |
throws IOException { |
| 565 |
|
| 566 |
HttpEntity httpEntity = executePost(url, new ByteArrayEntity(datapost), |
| 567 |
getHeaderParameters(this.getToken(), contentType)); |
| 568 |
return GooglePlay.ResponseWrapper.parseFrom(httpEntity.getContent()); |
| 569 |
|
| 570 |
} |
| 571 |
|
| 572 |
/** |
| 573 |
* Executes POST request on given URL with POST parameters and header |
| 574 |
* parameters. |
| 575 |
*/ |
| 576 |
private HttpEntity executePost(String url, String[][] postParams, String[][] headerParams) |
| 577 |
throws IOException { |
| 578 |
|
| 579 |
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); |
| 580 |
|
| 581 |
for (String[] param : postParams) { |
| 582 |
if (param[0] != null && param[1] != null) { |
| 583 |
formparams.add(new BasicNameValuePair(param[0], param[1])); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8"); |
| 588 |
|
| 589 |
return executePost(url, entity, headerParams); |
| 590 |
} |
| 591 |
|
| 592 |
/** |
| 593 |
* Executes POST request on given URL with {@link HttpEntity} typed POST |
| 594 |
* parameters and header parameters. |
| 595 |
*/ |
| 596 |
private HttpEntity executePost(String url, HttpEntity postData, String[][] headerParams) |
| 597 |
throws IOException { |
| 598 |
HttpPost httppost = new HttpPost(url); |
| 599 |
|
| 600 |
if (headerParams != null) { |
| 601 |
for (String[] param : headerParams) { |
| 602 |
if (param[0] != null && param[1] != null) { |
| 603 |
httppost.setHeader(param[0], param[1]); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
httppost.setEntity(postData); |
| 609 |
|
| 610 |
return executeHttpRequest(httppost); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Executes GET request on given URL with GET parameters and header |
| 615 |
* parameters. |
| 616 |
*/ |
| 617 |
private HttpEntity executeGet(String url, String[][] getParams, String[][] headerParams) |
| 618 |
throws IOException { |
| 619 |
|
| 620 |
if (getParams != null) { |
| 621 |
List<NameValuePair> formparams = new ArrayList<NameValuePair>(); |
| 622 |
|
| 623 |
for (String[] param : getParams) { |
| 624 |
if (param[0] != null && param[1] != null) { |
| 625 |
formparams.add(new BasicNameValuePair(param[0], param[1])); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
url = url + "?" + URLEncodedUtils.format(formparams, "UTF-8"); |
| 630 |
} |
| 631 |
|
| 632 |
HttpGet httpget = new HttpGet(url); |
| 633 |
|
| 634 |
if (headerParams != null) { |
| 635 |
for (String[] param : headerParams) { |
| 636 |
if (param[0] != null && param[1] != null) { |
| 637 |
httpget.setHeader(param[0], param[1]); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
return executeHttpRequest(httpget); |
| 643 |
} |
| 644 |
|
| 645 |
/** Executes given GET/POST request */ |
| 646 |
private HttpEntity executeHttpRequest(HttpUriRequest request) throws ClientProtocolException, |
| 647 |
IOException { |
| 648 |
|
| 649 |
HttpResponse response = getClient().execute(request); |
| 650 |
|
| 651 |
if (response.getStatusLine().getStatusCode() != 200) { |
| 652 |
throw new GooglePlayException(new String(Utils.readAll(response.getEntity().getContent()))); |
| 653 |
} |
| 654 |
|
| 655 |
return response.getEntity(); |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
* Gets header parameters for GET/POST requests. If no content type is given, |
| 660 |
* default one is used! |
| 661 |
*/ |
| 662 |
private String[][] getHeaderParameters(String token, String contentType) { |
| 663 |
|
| 664 |
return new String[][] { |
| 665 |
{ "Accept-Language", getLocalization() != null ? getLocalization() : "en-EN" }, |
| 666 |
{ "Authorization", "GoogleLogin auth=" + token }, |
| 667 |
{ "X-DFE-Enabled-Experiments", "cl:billing.select_add_instrument_by_default" }, |
| 668 |
{ |
| 669 |
"X-DFE-Unsupported-Experiments", |
| 670 |
"nocache:billing.use_charging_poller,market_emails,buyer_currency,prod_baseline,checkin.set_asset_paid_app_field,shekel_test,content_ratings,buyer_currency_in_app,nocache:encrypted_apk,recent_changes" }, |
| 671 |
{ "X-DFE-Device-Id", this.getAndroidID() }, |
| 672 |
{ "X-DFE-Client-Id", "am-android-google" }, |
| 673 |
{ "User-Agent", getUseragent() }, |
| 674 |
{ "X-DFE-SmallestScreenWidthDp", "320" }, |
| 675 |
{ "X-DFE-Filter-Level", "3" }, |
| 676 |
{ "Host", "android.clients.google.com" }, |
| 677 |
{ |
| 678 |
"Content-Type", |
| 679 |
(contentType != null) ? contentType |
| 680 |
: "application/x-www-form-urlencoded; charset=UTF-8" } }; |
| 681 |
} |
| 682 |
|
| 683 |
public String getToken() { |
| 684 |
return token; |
| 685 |
} |
| 686 |
|
| 687 |
public void setToken(String token) { |
| 688 |
this.token = token; |
| 689 |
} |
| 690 |
|
| 691 |
public String getAndroidID() { |
| 692 |
return androidID; |
| 693 |
} |
| 694 |
|
| 695 |
public void setAndroidID(String androidID) { |
| 696 |
this.androidID = androidID; |
| 697 |
} |
| 698 |
|
| 699 |
public String getSecurityToken() { |
| 700 |
return securityToken; |
| 701 |
} |
| 702 |
|
| 703 |
public void setSecurityToken(String securityToken) { |
| 704 |
this.securityToken = securityToken; |
| 705 |
} |
| 706 |
|
| 707 |
public HttpClient getClient() { |
| 708 |
return client; |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* Sets {@link HttpClient} instance for internal usage of GooglePlayAPI. It is |
| 713 |
* important to note that this instance should allow concurrent connections. |
| 714 |
* |
| 715 |
* @see getConnectionManager |
| 716 |
* |
| 717 |
* @param client |
| 718 |
*/ |
| 719 |
public void setClient(HttpClient client) { |
| 720 |
this.client = client; |
| 721 |
} |
| 722 |
|
| 723 |
public String getEmail() { |
| 724 |
return email; |
| 725 |
} |
| 726 |
|
| 727 |
public void setEmail(String email) { |
| 728 |
this.email = email; |
| 729 |
} |
| 730 |
|
| 731 |
public String getLocalization() { |
| 732 |
return localization; |
| 733 |
} |
| 734 |
|
| 735 |
/** |
| 736 |
* Localization string that will be used in each request to server. Using this |
| 737 |
* option you can fetch localized informations such as reviews and |
| 738 |
* descriptions. |
| 739 |
* <p> |
| 740 |
* Note that changing this value has no affect on localized application list |
| 741 |
* that server provides. It depends on only your IP location. |
| 742 |
* <p> |
| 743 |
* |
| 744 |
* @param localization |
| 745 |
* can be <b>en-EN, en-US, tr-TR, fr-FR ... (default : en-EN)</b> |
| 746 |
*/ |
| 747 |
public void setLocalization(String localization) { |
| 748 |
this.localization = localization; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* @return the useragent |
| 753 |
*/ |
| 754 |
public String getUseragent() { |
| 755 |
return useragent; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* @param useragent |
| 760 |
* the useragent to set |
| 761 |
*/ |
| 762 |
public void setUseragent(String useragent) { |
| 763 |
this.useragent = useragent; |
| 764 |
} |
| 765 |
|
| 766 |
} |