【实例图文详解】OAuth 2.0 for Web Server Applications
原文鏈接:http://blog.csdn.net/hjun01/article/details/42032841? ? ??
?
?OAuth 2.0 for Web Server Applications, verifying a user's?Android?in-app subscription
????????
??????? 在寫本文之前先說些題外話。
? ? ? ? 前段時(shí)間游戲急于在GoolePlay上線,明知道如果不加Auth2.0的校驗(yàn)是不安全的還是暫時(shí)略過了這一步,果然沒幾天就發(fā)現(xiàn)后臺(tái)記錄與玩家實(shí)際付費(fèi)不太一致,懷疑有玩家盜刷游戲元寶等,并且真實(shí)的走過了GooglePlay的所有支付流程完成道具兌換,時(shí)間一長(zhǎng)嚴(yán)重性可想而知。經(jīng)過查閱大量google官方文檔后把代碼補(bǔ)上,并在這里記錄下OAuth 2.0 的使用,Google提供了OAuth2.0的好幾種使用用途,每種使用方法都有些不同,具體可以看下這篇博客。在這里只寫OAuth 2.0 for Web Server Applications的使用,涉及refresh_token, access_token等的獲取和使用,以及如何向google發(fā)送GET和POST請(qǐng)求等,最終完成用戶在安卓應(yīng)用內(nèi)支付購(gòu)買信息的校驗(yàn)。
? ? ? ? 先貼下關(guān)于Using OAuth 2.0 for Web Server Applications的解釋的谷歌官方原文:
? ? ? ? The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the type of access being requested. As in other scenarios, Google handles user authentication, session selection, and user consent. The result is an authorization code, which Google returns to your application in a query string.
? ? ? ? After receiving the authorization code, your application can exchange the code (along with a client ID and client secret) for an access token and, in some cases, a refresh token.
? ? ? ? The application can then use the access token to access a Google API.
? ? ? ? If a refresh token is present in the authorization code exchange, then it can be used to obtain new access tokens at any time. This is called offline access, because the user does not have to be present at the browser when the application obtains a new access token.
通過原文和圖解我們可以知道這樣一個(gè)流程(下文會(huì)詳細(xì)說明):
? ? ? ??一. 在Google Developer Console中創(chuàng)建一個(gè)?Web Application賬戶,得到client_id,client_secret 和?redirect_uri,這3個(gè)參數(shù)后邊步驟常用到(此為前提)
? ? ? ? 二. 獲取Authorization code?
? ? ? ? 三. 利用code 獲取access_token,refresh_token
? ? ? ? 四. 進(jìn)一步可利用refresh_token獲取新的access_token
? ? ? ? 五. 使用access_token 調(diào)用Google API 達(dá)到最終目的(如果access_token過時(shí),回到第四步)
? ? ? ? 需注意的是:在第三步操作,當(dāng)我們第一次利用code獲取access_token時(shí),谷歌會(huì)同時(shí)返回給你一個(gè)refresh_token,以后再次用code獲取access_token操作將不會(huì)再看到refresh_token,所以一定要保存下來。這個(gè)refresh_token是長(zhǎng)期有效的,如果沒有明確的被應(yīng)用管理者撤銷是不會(huì)過期的,而access_token則只有3600秒的時(shí)效,即1個(gè)小時(shí),那么問題來了,access_token和refresh_token是什么關(guān)系?很明顯的,我們最終是要使用access_token 去調(diào)用Google API,而access_token又有時(shí)效限制,所以當(dāng)access_token過期后,我們可以用長(zhǎng)效的refresh_token去再次獲取access_token,并且可以可以在任何時(shí)間多次獲取,沒有次數(shù)限制。其實(shí)當(dāng)我們得到refresh_token后,就是一個(gè)轉(zhuǎn)折點(diǎn)。 ?
?
? ? ? ? 下面詳細(xì)分解步驟:
一、在Google Developer Console中創(chuàng)建一個(gè)Web application賬戶
? ? (這里使用的是新版的Google Developer Console頁面,其實(shí)可在Account settings中設(shè)置為中文顯示~)
? ? ? ??
? ? ? ? 其中4.可以隨意填寫。創(chuàng)建完成后可以看下下圖所示:
?
? ? ? ? 在這里我們拿到3個(gè)關(guān)鍵參數(shù):?client_id,client_secret,redirect_uris,,于下邊步驟。
可能會(huì)有人有疑問,怎么就能確定在google developer console 建立的project就于Googleplay上線的安卓應(yīng)用有關(guān)聯(lián)呢?為什么可以用這些參數(shù)得來的access_token去調(diào)用谷歌API?其實(shí)在Googleplay發(fā)布應(yīng)用時(shí)就有關(guān)聯(lián)project的操作,之后創(chuàng)建project的人可以給其他谷歌賬戶授權(quán),這樣其他谷歌賬戶可以在自己的developer console頁面直接看到該project和以下的web application等, 并且可在下一步操作中登錄自己的谷歌賬戶獲取code。
?
二. 獲取Authorization code
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher &response_type=code &access_type=offline &redirect_uri={REDIRECT_URIS} &client_id={CLIENT_ID}? ? ? ? 我們需要將這個(gè)URL以瀏覽器的形式打開,這時(shí)會(huì)跳出提示你Sign in with your Google Account,然后在用有project授權(quán)的谷歌賬戶登錄,地址欄會(huì)出現(xiàn)我們所需的code。例如:https://www.example.com/oauth2callback?code=4/CpVOd8CljO_gxTRE1M5jtwEFwf8gRD44vrmKNDi4GSS.kr-GHuseD-oZEnp6UADFXm0E0MD3FlAI
?
三. 利用code 獲取access_token,refresh_token
https://accounts.google.com/o/oauth2/token? code={CODE} &client_id={CLIENT_ID} &client_secret={CLIENT_SECRET} &redirect_uri={REDIRECT} &grant_type=authorization_code? ? ? ? 我們這一步的目的是獲取refresh_token,只要有了這個(gè)長(zhǎng)效token,access_token是隨時(shí)可以獲取的,第一次發(fā)起請(qǐng)求得到的JSON字符串如下所示,以后再請(qǐng)求將不再出現(xiàn)refresh_token,要保存好。expires_in是指access_token的時(shí)效,為3600秒。
{ "access_token": "ya29.3gC2jw5vm77YPkylq0H5sPJeJJDHX93Kq8qZHRJaMlknwJ85595eMogL300XKDOEI7zIsdeFEPY6zg", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "1/FbQD448CdDPfDEDpCy4gj_m3WDr_M0U5WupquXL_o" }? ? ? ???
四. 進(jìn)一步可利用refresh_token獲取新的access_token
https://accounts.google.com/o/oauth2/token? grant_type=refresh_token &client_id={CLIENT_ID} &client_secret={CLIENT_SECRET} &refresh_token={REFRESH_TOKEN}? ? ? ? 這里我們要向谷歌發(fā)起POST請(qǐng)求,Java代碼如下:
[java]?view plain?copy?print?
五. 使用access_token 調(diào)用Google API 達(dá)到最終目的(如果access_token過時(shí),回到第四步)
? ? ? ? 在這里我所需要獲取的是我在應(yīng)用內(nèi)給GooglePlay支付的購(gòu)買信息,此類信息包含以下幾個(gè)屬性:(可參考Google Play Developer API下的Purchases.products)
? ? ? ? A ProductPurchase resource indicates the status of a user's inapp product purchase.
{"kind": "androidpublisher#productPurchase", ? "purchaseTimeMillis": long, ? "purchaseState": integer, (purchased:0 cancelled:1,我們就是依靠這個(gè)判斷購(gòu)買信息) ? "consumptionState": integer, ? "developerPayload": string }? ? ? ? 帶著access_token參數(shù)向GoogleApi發(fā)起GET請(qǐng)求,Java代碼如下:
[java]?view plain?copy?print?? ? ? ??
? ? ? ? 到這里就寫完了,如果有什么疑問可以留言。
? ? ? ??另外,iOS應(yīng)用內(nèi)支付,蘋果商店AppStore購(gòu)買信息校驗(yàn)的博客在這里:http://blog.csdn.net/hjun01/article/details/44039939
?
總結(jié)
以上是生活随笔為你收集整理的【实例图文详解】OAuth 2.0 for Web Server Applications的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简单编译安装Apache
- 下一篇: centos 7 密码破解 rm -r