[Android] Google IAP unmaneged items服务器校验
android IAP unmaneged items 服务器校验
当成功IAP以后, 会在google服务器记录此次购买的状态. 可以通过Google Play Android Developer API去请求此状态, 从而完成校验和发给玩家相应的道具.
1> 客户端字串, orderId(订单ID), productId(购买道具名), packageName(APP包名), purchaseToken(token, 唯一值), 此4个串是校验需要用到的, 传给服务器.
2> 调用Google Play Android Developer API(https://developers.google.com/android-publisher/api_usage) 需要使用OAuth2.0, 可以采用Java, Python, .Net, Ruby, PHP等(https://developers.google.com/identity/protocols/OAuth2WebServer)
本文使用python实现.
3> 设置环境
在console.developers.google.com启用Google Play Android Developer API接口
在play.google.com中设置API权限, OAUTH客户端项目设置
4> 脚本实现
import httplib2 import pprint import sys import time import os import MySQLdb list_bill=[] from apiclient.discovery import build from oauth2client.client import SignedJwtAssertionCredentials def main(argv): # connect the db to get bill db=MySQLdb.connect("localhost","root","pass",sys.argv[1]) cursor=db.cursor() try: # 查询客户端传来的字串 cursor.callproc(‘getbill‘,(‘2‘)) results=cursor.fetchall() while(cursor.nextset()): print "111111111111" for result in results: # Load the key in PKCS 12 format that you downloaded from the Google API # Console when you created your Service account. f = file(‘console中的p12 key的路径‘, ‘rb‘) key = f.read() f.close() # Create an httplib2.Http object to handle our HTTP requests and authorize it # with the Credentials. Note that the first parameter, service_account_name, # is the Email address created for the Service account. It must be the email # address associated with the key that was created. credentials = SignedJwtAssertionCredentials( ‘play.google.com中OAUTH授权账号‘, key, scope=‘https://www.googleapis.com/auth/androidpublisher‘) http = httplib2.Http() http = credentials.authorize(http) #service build service = build("androidpublisher", "v2", http=http) #get bill list_bill=result[4].split(‘ ‘) transaction_id=list_bill[0] product_id=list_bill[1] packagename=list_bill[2] token=list_bill[3] try: print "try to get" lists = service.purchases().products().get(packageName=packagename,productId=product_id,token=token).execute(http=http) except: # bill is missing or invalid bill cursor.callproc(# sql处理代码) while(cursor.nextset()): print "111111111111" db.commit() continue pprint.pprint(lists) # 判断是否是合法且未消费 if(lists[‘purchaseState‘]==0 and lists[‘consumptionState‘]==0): diff=time.time()-float(lists[‘purchaseTimeMillis‘][0:10]) if(diff>2592000): #over time bill, record the log cursor.callproc(# sql处理代码) while(cursor.nextset()): print "111111111111" db.commit() continue else: #good receipt num=product_id.split(‘.‘)[3] cursor.callproc(‘check_bill‘,(result[0],1,num,time.time(),‘‘,transaction_id)) while(cursor.nextset()): print "111111111111" db.commit() continue # 已消费 elif(lists[‘purchaseState‘]==0 and lists[‘consumptionState‘]==1): cursor.callproc(# sql处理代码) while(cursor.nextset()): print "111111111111" db.commit() print "Already consumed" continue except: # sql get is wrong print "sql err" finally: cursor.close() db.close() if __name__ == ‘__main__‘: while(1): if(os.path.exists("/tmp/stop_gp_iap_check-"+sys.argv[1]+".txt")): print "stop" break else: main(sys.argv) time.sleep(2)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。