准備工作
# Python3.x的開發環境
# google的賬號 https://developers.google.com/
# 各種語言的library https://developers.google.com/api-client-library/
# 需要安裝的模塊
1. pip install google-api-python-client
2. pip install google-auth-oauthlib
2. pip install oauth2client
打開谷歌開發者中心 用到的api是 Google Play Android Developer API
選擇你上傳apk 的項目(沒有的可以創建項目不過是沒有評價信息的)
1.進入要使用的api Google Play Android Developer API 創建憑證
創建訪問憑證
我們要使用的是紅框中的選項
其他的作用 分別是web 服務端, web前端,移動端,谷歌瀏覽器應用 以及各種桌面應用程序,我們要使用的是是 不可視的腳本調用
創建完成后回到下面選項
需要下載秘鑰憑證 有兩種格式 json和p12 格式的文件 推薦使用json,由於本人需要兼容 舊項目所以使用的是p12格式的文件
導出下載文件
1.打開 Python 操作 google API 的demo 可以參考一下 地址是
https://github.com/googlesamples/android-play-publisher-api
調用 相關的api接口參考文檔官方地址
https://developers.google.com/apis-explorer/?hl=zh_CN#p/androidpublisher/v3/
如上圖 :
androidpublisher.reviews.get 是獲取單個評論
androidpublisher.reviews.list 是獲取評論集合,但只能獲取上周的
androidpublisher.reviews.reply 回復的接口
注意:由於您對評論的回復會在應用商店頁面上公開顯示,因此在撰寫這些回復時,不要包含有關用戶的敏感信息,這一點很重要。
注意:androidpublisher.reviews.reply POST
請求,最多可以包含350個字符。您應該在回復中使用純文本; 格式良好的HTML標記將被刪除,並且不會包含在您的回復字符數中。但是,保留在格式良好的HTML標記內的內容。
需要注意的是 這個文檔有點舊了 有些參數不太用了 比如 androidpublisher.reviews.list 參數 startIndex比較坑
code
1 #!/usr/bin/python 2 3 """Lists all the apks for a given app.""" 4 5 import argparse 6 7 from apiclient.discovery import build 8 import httplib2 9 from oauth2client import client 10 11 # 你的角色郵件用戶名 12 SERVICE_ACCOUNT_EMAIL = ('ggapi-sacc-elva@api-project-866561535012.iam.gserviceaccount.com') 13 14 15 def main(): 16 # 官方文檔用的是file 我改為open 17 f = open('1.p12', 'rb') 18 key = f.read() 19 f.close() 20 credentials = client.SignedJwtAssertionCredentials( SERVICE_ACCOUNT_EMAIL, key, scope='https://www.googleapis.com/auth/androidpublisher') 21 http = httplib2.Http() 22 http = credentials.authorize(http) 23 24 # 加載要使用的api 名稱 25 service = build('androidpublisher', 'v3', http=http) 26 # 包名 27 package_name = 'com.longtech.lastwars.gp'#flags.package_name 28 29 try: 30 31 # edit_request = service.edits().insert(body={}, packageName=package_name),fields='pageInfo/totalResults,reviews' 32 # 默認第一頁 33 pllist=service.reviews().list(maxResults=20,packageName=package_name) 34 # result = edit_request.execute() 35 pllistresult = pllist.execute() 36 for item in pllistresult['reviews']: 37 print(item) 38 39 40 token=pllistresult['tokenPagination']['nextPageToken'] 41 # 翻頁 不傳入token是第一頁 42 pllist=service.reviews().list(maxResults=20,packageName=package_name,token=token) 43 pllistresult = pllist.execute() 44 print(pllistresult) 45 # edit_id = result['id'] 46 47 # apks_result = service.edits().apks().list( 48 # editId=edit_id, packageName=package_name).execute() 49 50 # for apk in apks_result['apks']: 51 # print ('versionCode: %s, binary.sha1: %s' % (apk['versionCode'], apk['binary']['sha1'])) 52 53 except client.AccessTokenRefreshError: 54 print ('The credentials have been revoked or expired, please re-run the ' 55 'application to re-authorize') 56 57 if __name__ == '__main__': 58 main()