Skip to content

userInfoUrl is outdated - just update the version to the current #2

@speckmoor

Description

@speckmoor

First of all it is an amazing Tutorial, one of the best I have read over the years. Thank you so much for covering the OAuth Topics.

When implementing OAuth with google I ran into the following exception:

Field 'locale' is required for type with serial name '.UserInfo', but it was missing at path: $ for request https://www.googleapis.com/oauth2/**v1**/userinfo"

Just update the version please and additional to that I changed the getUserInfo function to be more robust about any API changes from Google.

suspend fun getUserInfo(accessToken: String, oauthConfig: OAuthConfig, httpClient: HttpClient): UserInfo { val response = httpClient.get(oauthConfig.userInfoUrl) { headers { append("Authorization", "Bearer $accessToken") } } val jsonObject = Json.decodeFromString<JsonObject>(response.bodyAsText()) return UserInfo( id = jsonObject["sub"]?.jsonPrimitive?.contentOrNull ?: "", email = jsonObject["email"]?.jsonPrimitive?.contentOrNull, name = jsonObject["name"]?.jsonPrimitive?.contentOrNull, picture = jsonObject["picture"]?.jsonPrimitive?.contentOrNull, familyName = jsonObject["picture"]?.jsonPrimitive?.contentOrNull, givenName = jsonObject["picture"]?.jsonPrimitive?.contentOrNull, locale = jsonObject["picture"]?.jsonPrimitive?.contentOrNull )

(You need to change the Types in the data class to String? as well but Idea will tell you that.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions