[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)
[Android] Google IAP unmaneged items服务器校验的更多相关文章
- SDK接入(2)之Android Google Play内支付(in-app Billing)接入
SDK接入(2)之Android Google Play内支付(in-app Billing)接入 继上篇SDK接入(1)之Android Facebook SDK接入整理完Facebook接入流程之 ...
- Android Webview SSL 自签名安全校验解决方案
服务器证书校验主要针对 WebView 的安全问题. 在 app 中需要通过 WebView 访问 url,因为服务器采用的自签名证书,而不是 ca 认证,使用 WebView 加载 url 的时候会 ...
- 转: android之虚拟机访问tomcat服务器资源
最近在研究Android虚拟机访问tomcat服务器资源,所以找了个时间写下这篇博客和大家分享一下心得. 其实Android虚拟机访问tomcat服务器非常的简单,只要不要弄错IP地址就可以访问tom ...
- Android Google 地图 API for Android
从健康类 app Runkeeper 到游戏 app 精灵宝可梦,位置服务对现代 app 来说越来越重要. 在本文中,我们将创建一个 app,名字就叫做 City Guide.这个 app 允许用户搜 ...
- Android操作HTTP实现与服务器通信(转)
Android操作HTTP实现与服务器通信 本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...
- Android提交数据到JavaWeb服务器实现登录
之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互.虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的.现在我先来展示一下我的成果 ...
- android文件上传到服务器
package uploadDemo; import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStre ...
- android上传文件到服务器
package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...
- Android google map 两点之间的距离
在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离: public class DistanceHelper { /** Names for the ...
随机推荐
- jsp页面传参大汇总-转帖收藏
http://blog.csdn.net/ssy_shandong/article/details/9328985/
- 开始学习c语言
学习c语言的第一篇博客,心里的感觉说不出来,不知道能不能坚持下去,我是一名工作了差不多2年的phper,其实我本来是想学习数据结构和算法的,但是尼玛这年头那些书的例子都不是php,动不动就是c,jav ...
- Transaction Save Point (SET XACT_ABORT { ON | OFF })
ref:http://blog.csdn.net/wym3587/article/details/6940630 ref:http://www.cnblogs.com/jiajiayuan/archi ...
- 在Eclipse 中使用tomcat8
最新版本的Eclipse让人不支持 Tomcat 8,该方法让Eclipse能使用Tomcat8 第一步:下载最新版本Eclipse for Java EE 第二步:到WTP downloads pa ...
- 多数浏览器默认会缓存input的值,只有使用ctl+F5强制刷新的才可以清除缓存记录。
如果不想让浏览器缓存input的值,有2种方法: 方法一: 在不想使用缓存的input中添加 autocomplete="off"; eg: <input type=&quo ...
- Mysql复合索引
当Mysql使用索引字段作为条件时,如果该索引是复合索引,必须使用该索引中的第一个字段作为条件才能保证系统使用该索引,否则该索引不会被使用,并且应尽可能地让索引顺序和字段顺序一致
- Javascript之confirm的用法
confirm函数 confirm函数用于提供确认功能,它首先显示给定的message参数所包含的信息,并提供两个可选择的回答“ok”和“cancel”,然后等待用户选择其中的一个.如果用户选择“ok ...
- 项目中如何GB2312转UTF-8
$str = mb_convert_encoding($str, "gb2312", "UTF-8"); // 这是一个PHP 自带函数 参数1 是要转的字符, ...
- JAVA中的异常及处理异常的方法
异常 这是我老师的喜好:就是说一上来就拿一张图给大家看看,过过瘾-_- 这是一张: 异常分类图 来,这里还有一张带中文的常见异常截图!!! 1:先来说说什么是异常吧: 其实就是"阻止当前方法 ...
- [转] 经典SQL练习题
原题目来自qaz13177_58_CSDN博客 http://blog.csdn.net/qaz13177_58_/article/details/5575711/#sql 只是更新个人答案供参考 表 ...