在接入google的SDK之前,當然先要用你的google開發者賬號要去申請你接入的應用,這些步驟就直接省略了具體的步驟可以查看這篇博文:http://blog.csdn.net/hjun01/article/details/42032841 里面有比較詳細的介紹,這里只是簡單的介紹下步驟流程僅供參考。
1.google賬號登錄服務器端驗證過程
1).客戶端發送id_token到服務器端
2).服務器端發送post請求到Google:
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={XYZ123}
3).請求成功,返回如下格式的:
{
// These six fields are included in all Google ID Tokens.
"iss": "https://accounts.google.com",
"sub": "110169484474386276334",
"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"iat": "1433978353",
"exp": "1433981953",
// These seven fields are only included when the user has granted the "profile" and
// "email" OAuth scopes to the application.
"email": "testuser@gmail.com",
"email_verified": "true",
"name" : "Test User",
"picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
"given_name": "Test",
"family_name": "User",
"locale": "en"
}
詳情請查看google的開發者官網。
2.接入google支付
Google支付驗證流程簡介
一. 在Google Developer Console中創建一個 Web Application賬戶,得到client_id,client_secret
和 redirect_uri,這3個參數后邊步驟常用到(此為前提)
二. 獲取Authorization code(獲取授權碼)
發送get請求
https://accounts.google.com/o/oauth2/auth?
scope=https://www.googleapis.com/auth/androidpublisher
&response_type=code
&access_type=offline
&redirect_uri={...}&client_id={...}
將會返回如下:
https://www.example.com/oauth2callback?
code=4/CpVOd8CljO_gxTRE1M5jtwEFwf8gRD44vrmKNDi4GSS.kr-GHuseD-oZEnp6UADFXm0E0MD3FlAI
三. 利用code(上一步獲得的code) 獲取access_token,refresh_token
發送post請求
https://accounts.google.com/o/oauth2/token?
grant_type=authorization_code
code={the code from the previous step}
client_id={the client ID token created in the APIs Console}
client_secret={the client secret corresponding to the client ID}
redirect_uri={the URI registered with the client ID}
我們這一步的目的是獲取refresh_token,只要有了這個長效token,access_token是隨時可以獲取的,
第一次發起請求得到的JSON字符串如下所示,以后再請求將不再出現refresh_token,要保存好。expires_in
是指access_token的時效,為3600秒。
{
"access_token": "ya29.3gC2jw5vm77YPkylq0H5sPJeJJDHX93Kq8qZHRJaMlknwJ85595eMogL300XKDOEI7zIsdeFEPY6zg",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "1/FbQD448CdDPfDEDpCy4gj_m3WDr_M0U5WupquXL_o"
}
四. 進一步可利用refresh_token獲取新的access_token
發送post請求:https://accounts.google.com/o/oauth2/token?grant_type=refresh_token&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}&refresh_token={REFRESH_TOKEN}
A successful response will contain another access token:
{
"access_token" : "ya29.AHES3ZQ_MbZCwac9TBWIbjW5ilJkXvLTeSl530Na2",
"token_type" : "Bearer",
"expires_in" : 3600,
}
五. 使用access_token 調用Google API 達到最終目的(如果access_token過時,回到第四步)
發送get請求:https://www.googleapis.com/androidpublisher/v2/applications/{packageName}/purchases/products/{productId}/tokens/{purchaseToken}}?access_token={access_token}
成功返回:
{
"kind": "androidpublisher#productPurchase",
"purchaseTimeMillis": long,
"purchaseState": integer, (purchased:0 cancelled:1,我們就是依靠這個判斷購買信息)
"consumptionState": integer,
"developerPayload": string
}
google官方關於登錄或者支付都有相應編程語言的API,用API來實現更加簡單就沒有這么復雜了,如果沒有相應編程語言的API可以到github上搜索,一般都可以找到。