Skip to content

Instantly share code, notes, and snippets.

@nk2IsHere
Last active May 26, 2020 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nk2IsHere/c706be474db8fca11733c9363d2a87f8 to your computer and use it in GitHub Desktop.
Save nk2IsHere/c706be474db8fca11733c9363d2a87f8 to your computer and use it in GitHub Desktop.
Get user's google profile by token in Kotlin
implementation("com.google.auth:google-auth-library-oauth2-http:0.20.0")
implementation("com.google.http-client:google-http-client-gson:1.35.0")
implementation("com.google.apis:google-api-services-oauth2:v2-rev157-1.25.0")
// <...>
// assuming:
// userToken: String - received token from client
private val googleNetHttpTransport = GoogleNetHttpTransport.newTrustedTransport()
private val googleJsonFactory = GsonFactory()
private val oauth2Api = Oauth2.Builder(
googleNetHttpTransport,
googleJsonFactory,
HttpCredentialsAdapter(OAuth2Credentials.create(
AccessToken(userToken, null)
))
)
.build()
private val userInfo = oauth2Api.Userinfo().v2().Me().get()
.execute()
println(userInfo)
// check for class info here:
// https://developers.google.com/resources/api-libraries/documentation/oauth2/v2/csharp/latest/classGoogle_1_1Apis_1_1Oauth2_1_1v2_1_1Data_1_1Userinfo.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment