码迷,mamicode.com
首页 > 移动开发 > 详细

[Android] Google IAP unmaneged items服务器校验

时间:2015-04-13 16:16:51      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:

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)

  

      

[Android] Google IAP unmaneged items服务器校验

标签:

原文地址:http://www.cnblogs.com/randywithj/p/4422071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!