伺服器谷歌訂單驗證 - server account - json
阿新 • • 發佈:2020-12-17
技術標籤:java
伺服器接入谷歌訂單驗證
上架谷歌,需要在伺服器端接入谷歌的訂單驗證,頗費了一番功夫。
流程
經過一番嘗試,最終使用server account - json檔案方式接入。關於如何生成對應的json檔案,網上有相應的教程。
注意:要使用關聯專案的主賬號生成json檔案。
程式碼步驟
GoogleCredential credential =GoogleCredential .fromStream(new FileInputStream(FireBaseConstants.LOCAL_FOLDER+"google-Japan.json")).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER)); credential.refreshToken(); accessToken= credential.getAccessToken();
關於Collections.singleton方法裡的引數,
官方給的API裡有寫的是SQLAdminScopes.SQLSERVICE_ADMIN,
但是出現了403:Request had insufficient authentication scopes.的錯誤,
換成AndroidPublisherScopes.ANDROIDPUBLISHER就好了,
如果AndroidPublisherScopes.ANDROIDPUBLISHER不行的話,換回去試試。
注意:一定要呼叫refreshToken方法,不然啥也拿不到。
String queryUrl = "https://www.googleapis.com/androidpublisher/v3/applications/" + packageName + "/purchases/products/" + productId + "/tokens/" + token; queryUrl += "?access_token=" + accessToken;
然後把前一步拿到的access Token放到請求裡面,去谷歌訪問就能拿到相應的谷歌訂單資料了。
引數:packageName - 包名,productId - 產品Id,token - 谷歌返回的支付成功的purchaseToken