Last active
May 26, 2020 16:58
-
-
Save nk2IsHere/c706be474db8fca11733c9363d2a87f8 to your computer and use it in GitHub Desktop.
Get user's google profile by token in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <...> | |
// 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