본문 바로가기

구글 안드로이드/기타

Getting the Android Market Token

Tuesday, September 15th, 2009 | Author: Tim

Mmmmm... Market Data...

Mmmmm... Market Data...


I’ve been sitting on this code for a while now. I had been tooling around with it mostly about a year ago when I was collecting live market data - it actually took a long time to figure out what the actual token was. Not sure if this method will be changed since there is apparently a big update for the market coming up, I’m assuming it won’t change though - otherwise older versions of the market would break. For people who don’t know - the http requests the vending apk is sending holds a “token” which there was no link to how it was obtained or formed. It turns out to just be a simple token requested like any other google service. The “service” name just turned out to be “android”. Other “services” that the market uses are “androidsecure” and “sierra” - the latter being the codename for google-checkout.

The main reason this was a pain to figure out was because it’s being handled by the com.google.android.googleapps package, not the vending.apk package.

Hopefully this snippet will be useful for some people, like the MyMarket creators or anyone else trying to do market data analysis. I’ll try to post later how to actually send data and receive it using this token. Enjoy for now!

  1. import java.io.BufferedReader; 
  2. import java.io.BufferedWriter; 
  3. import java.io.FileNotFoundException; 
  4. import java.io.FileWriter; 
  5. import java.io.IOException; 
  6. import java.io.InputStreamReader; 
  7. import java.io.OutputStreamWriter; 
  8. import java.io.UnsupportedEncodingException; 
  9. import java.net.InetAddress; 
  10. import java.net.MalformedURLException; 
  11. import java.net.Socket; 
  12. import java.net.URL; 
  13. import java.net.URLConnection; 
  14. import java.net.URLEncoder; 
  15.  
  16. public class auth { 
  17.  
  18.     // declare variables for use in gettig auth-token 
  19.     private final static String account = "yourAccount@gmail.com"
  20.     private final static String password = "yourPassword"
  21.  
  22.     // service must be 'android' for market-data 
  23.     private final static String service = "android"
  24.  
  25.     public static void main(String[] args) throws UnsupportedEncodingException, MalformedURLException, IOException { 
  26.  
  27.         // Prepare data for being posted 
  28.         String rdata = URLEncoder.encode("accountType""UTF-8") + "=" + URLEncoder.encode("HOSTED_OR_GOOGLE""UTF-8"); 
  29.         rdata += "&" + URLEncoder.encode("Email""UTF-8") + "=" + URLEncoder.encode(account, "UTF-8"); 
  30.         rdata += "&" + URLEncoder.encode("Passwd""UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); 
  31.         rdata += "&" + URLEncoder.encode("service""UTF-8") + "=" + URLEncoder.encode(service, "UTF-8"); 
  32.  
  33.       // Send data 
  34.       URL url = new URL("https://www.google.com/accounts/ClientLogin"); 
  35.       URLConnection conn = url.openConnection(); 
  36.       conn.setDoOutput(true); 
  37.  
  38.       // Write post 
  39.       OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
  40.       wr.write(rdata); 
  41.       wr.flush(); 
  42.  
  43.       // Get the response 
  44.       BufferedReader rd; 
  45.       String line; 
  46.       StringBuffer resp = new StringBuffer(); 
  47.       try { 
  48.           rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
  49.           while ((line = rd.readLine()) != null) { 
  50.               resp.append(line); 
  51.               System.out.println(line); 
  52.           } 
  53.           wr.close(); 
  54.           rd.close(); 
  55.       } catch (FileNotFoundException e1) { 
  56.           // Catch bad url 
  57.           System.out.println("Error: Bad url address!"); 
  58.       } catch (IOException e1) { 
  59.           // Catch 403 (usually bad username or password 
  60.           if(e1.toString().contains("HTTP response code: 403")) 
  61.               System.out.println("Error: Forbidden response! Check username/password or service name."); 
  62.       } 
  63.  
  64.       String token = resp.toString().substring(resp.toString().indexOf("Auth=")+5); 
  65.  
  66.       try{ 
  67.            //Create file 
  68.           FileWriter fstream = new FileWriter("auth.token"); 
  69.               BufferedWriter out = new BufferedWriter(fstream); 
  70.           out.write(token); 
  71.           //Close the output stream 
  72.           out.close(); 
  73.           }catch (Exception e){//Catch exception if any 
  74.            System.err.println("Error: " + e.getMessage()); 
  75.           } 
  76.     } 


출처: http://strazzere.com/blog/?tag=android-market