2017年1月20日星期五

Google oAuth - part 2 - simple test




簡單的測試程式碼 :

<a href="https://accounts.google.com/o/oauth2/v2/auth?response_type=token&scope=email%20profile&state=%2Fprofile&
redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fraas%2Ftesting%2FgoogleOAuth2_redirectUri.jsp&
client_id=YOUR_Client_ID">Google login test </a>


YOUR_Client_ID , 記得要修改 , 
如何取得client_id ,請參考前一篇文章  Google oAuth - part 1 

redirect_uri 後面會用到


測試結果如下:
點擊以下“Google login test”,出現以下畫面(如果已經登入過google,否則會先出現登入畫面)

上圖  ,按下允許或按下取消 , 會到 redirect_uri 這個網址的網頁


在 redirect_uri 這個網頁,可以get token ( access_token )


按下取消 : 
  1. location:
    http://localhost:8080/raas/testing/googleOAuth2_redirectUri.jsp#error=access_denied&state=google

Validating the token

https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=YOUR_TOKEN
得到結果如下:
{
 "azp": "XXX.apps.googleusercontent.com",
 "aud": "XXXapps.googleusercontent.com",
 "sub": "XXX",
 "scope": "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile",
 "exp": "1484967676",
 "expires_in": "3542",
 "email": "XXX@gmail.com",
 "email_verified": "true",
 "access_type": "online"
}







google oAuth - part 1 - 取得Client ID











[Oauth]使用 OAuth 2.0 存取 Google APIs(for Login)


要使用Google API第一步要先 取得Client ID ,步驟如下:





  • 申建立專案





  •  啟用API




  • 啟用 Google+ API








  • 建立 憑證:



  • 類型 , 來源,"使用者資料" :



  •  已授權的重新導向 URI   (填寫需要的網址):
    • 例如:   
    • http://localhost:8080/raas/googleLoging.jsp
    • http://localhost:8080/raas/testing/googleOAuth2_redirectUri.jsp






  • 產品名稱:隨便填,以後可以修改



  • Client ID ,由google系統產生, source code會用到