How can I verify a Google authentication API access token?

前端 未结 10 531
长发绾君心
长发绾君心 2020-11-30 17:19

How can I verify a Google authentication access token?

I need to somehow query Google and ask: Is [given access token] valid for the [exampl

10条回答
  •  难免孤独
    2020-11-30 17:57

    For user check, just post get the access token as accessToken and post it and get the response

    https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=accessToken
    

    you can try in address bar in browsers too, use httppost and response in java also

    response will be like

    {
         "issued_to": "xxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
         "audience": "xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
         "user_id": "xxxxxxxxxxxxxxxxxxxxxxx",
         "scope": "https://www.googleapis.com/auth/userinfo.profile https://gdata.youtube.com",
         "expires_in": 3340,
         "access_type": "offline"
        }
    

    The scope is the given permission of the accessToken. you can check the scope ids in this link

    Update: New API post as below

    https://oauth2.googleapis.com/tokeninfo?id_token=XYZ123
    

    Response will be as

     {
     // 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"
    }
    

    For more info, https://developers.google.com/identity/sign-in/android/backend-auth

提交回复
热议问题