POST URL: https://checkout.google.com/api/checkout/v2/reports/Merchant/{merchantID}
在抓取 Report 前, 先說明如何找到後面程式需要的 Merchant ID 和 Merchant Key.
- 登入 https://checkout.google.com/sell/
- 切換到 [Settings] 頁籤, 然後按下位於左邊選單的 [Integration].
- 然後就能在右手邊的 [Account information] 看到兩個資訊: [Google merchant ID] 和 [Google merchant key]. (請將這兩個資訊記下來, 後面程式會用到)
參數名 | 參數值 | 說明 |
startDate | Format: yyyy-MM-ddTHH:mm:ss | 下單時間的查詢開始時間 |
endDate | Format: yyyy-MM-ddTHH:mm:ss | 下單時間的查詢結束時間 |
timeZone | Ex: America/New_York | 配合 startDate 與 endDate 所設定的 TimeZone 至於還有哪些可用, 可參考這個網頁 |
<order-list-request xmlns="http://checkout.google.com/schema/2" start-date={startDate} end-date={endDate} > <date-time-zone>{timeZone}</date-time-zone> </order-list-request>開始進入程式的部分, 一樣分三個部分: class 引用, 讀取資料, 處理資料(轉為 csv 檔)
- 此程式使用到的 class 如下:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.apache.commons.codec.binary.Base64;
- 從 Android Market 讀取資料的主程式如下:
public static void main(String[] args) { String strEmail = "xxxxx"; // google 帳號 String strPassword = "*****"; // google 密碼 String strMerchantID = "###############"; // Google merchant ID String strMerchantKEY = "**********************"; // Google merchant key String strAuthKey = ""; String strStartDate = "2011-05-01T00:00:00"; String strEndDate = "2011-05-02T00:00:00"; String strTimeZone = "Etc/GMT-8"; // 將檔案存在 D 槽, 並以查詢日期為檔名 String strOutFileName = String.format("D:\\Report_%s-%s.csv", strStartDate.replaceAll("[^0-9]", ""), strEndDate.replaceAll("[^0-9]", "")); try { String strMerchantInfo = strMerchantID + ":" + strMerchantKEY; byte[] encoded = Base64.encodeBase64(strMerchantInfo.getBytes()); // 設定驗證用的 Key strAuthKey = "Basic " + new String(encoded); // 組合查詢的 XML 字串 String strXmlRequest = "<order-list-request xmlns=\"http://checkout.google.com/schema/2\" " + "start-date=\"" + strStartDate + "\" end-date=\"" + strEndDate + "\">" + "<date-time-zone>" + strTimeZone + "</date-time-zone>" + "</order-list-request>"; // 進行資料傳送前的 Request 設定 URL url = new URL("https://checkout.google.com/api/checkout/v2/reports/Merchant/" + strMerchantID); HttpsURLConnection urlConnection = (HttpsURLConnection)url.openConnection(); urlConnection.setRequestProperty("Authorization", strAuthKey); urlConnection.setRequestProperty("Content-Type", "application/xml;charset=UTF-8"); urlConnection.setRequestProperty("Accept", "application/xml;charset=UTF-8"); urlConnection.setDoOutput(true); OutputStreamWriter oswOutput = null; try { // 進行資料傳送 oswOutput = new OutputStreamWriter(urlConnection.getOutputStream()); oswOutput.write(strXmlRequest); oswOutput.flush(); // 將傳回的資料轉為 csv 檔 getFile(urlConnection, strOutFileName); } catch (IOException e) { e.printStackTrace(); } } catch (Exception ex) { ex.printStackTrace(System.out); } }
- 另外撰寫一個副程式, 用來將傳回的資料轉成 csv 檔:
private static void getFile(HttpsURLConnection urlConnection, String strOutFileName) throws IOException { BufferedWriter bwFileOut = null; BufferedReader brIn = null; try { // 用 InputStreamReader 接收資料 brIn = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); // 寫入指定的檔案 bwFileOut = new BufferedWriter(new FileWriter(strOutFileName)); String line = ""; while ((line = brIn.readLine()) != null) { bwFileOut.write(line); bwFileOut.newLine(); } } catch (IOException e) { e.printStackTrace(); } finally { if (bwFileOut != null) { bwFileOut.flush(); bwFileOut.close(); } if (brIn != null) { brIn.close(); } } }
各位可再另外寫匯入的程式將其轉至資料庫中做為他用.
沒有留言:
張貼留言