[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 ...
随机推荐
- position
1.父集设置了relative,子集的absolute要设置top和left的值,不然父集的padding值会影响到子集的位置: #first{ width: 200px; height: 100px ...
- was not declared in this scope
“was not declared in this scope”是一个错误信息,在编译的时候会遇到.其含义为标识符在其出现的地方是未被定义的. 出现该错误的时候,会同时把未定义的变量名显示出来.比如如 ...
- Mybatis传多个参数(三种解决方案)
http://blog.csdn.net/liangyihuai/article/details/49965869 (zhuan)
- js字符串和正则表达式中的match、replace、exec等函数详解
正则并不是经常使用,而正则和字符串之间的函数关系又错综复杂,谁是谁的函数,又是怎么样的一种结果,往往我们是看一遍忘一遍,对此我是头疼不已,感觉自己是个笨蛋^_^. 为了以后不再查文档,特此把常用的函数 ...
- ES6里箭头函数的陷阱
ECMAScript 6新增了箭头函数 原来的匿名函数 function(){},现在可以简化成()=>{} 看起来高大上,像C#什么的语法. 但是箭头函数的this对象,不能更改,总是指向函数 ...
- ftp发送文件
#!/bin/bash #author:luyongjin IP=220.250.65.22 USERNAME='ftp_hangye20' PASSWORD='oUo2JD7oK#u-epw' #D ...
- JavaScriptCore 使用
JavaScriptCore JavaScriptCore是webkit的一个重要组成部分,主要是对JS进行解析和提供执行环境.代码是开源的,可以下下来看看(源码).iOS7后苹果在iPhone平台推 ...
- poj3069 Saruman's Army
http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...
- java中jdk和jre的区别
JRE: Java Runtime Environment JDK:Java Development Kit JRE顾名思义是java运行时环境,包含了java虚拟机,java基础类库.是使用java ...
- 基于webrtc的资源释放问题(二)
基于webrtc的资源释放问题(二) ——建立连接的过程中意外中断 应用背景: 我们在打电话的时候会不会遇到这种情况?打电话的时候未接通之前挂掉了电话,或者在接通之后建立的连接的过程中挂掉电话? 特别 ...