in apps/chat-android/app/src/main/java/com/amazonaws/services/chime/sdkdemo/data/source/service/DefaultAuthService.kt [134:166]
override suspend fun exchangeTokenForAwsCredential(accessToken: String): String? {
return withContext(ioDispatcher) {
val url = URL("${API_GATEWAY_INVOKE_URL}creds")
try {
val response = StringBuffer()
with(url.openConnection() as HttpURLConnection) {
requestMethod = "POST"
doInput = true
doOutput = true
setRequestProperty("Authorization", "Bearer $accessToken")
BufferedReader(InputStreamReader(inputStream)).use {
var inputLine = it.readLine()
while (inputLine != null) {
response.append(inputLine)
inputLine = it.readLine()
}
it.close()
}
if (responseCode == 200) {
response.toString()
} else {
logger.info(TAG, "Unable to exchange token. Response code: $responseCode")
null
}
}
} catch (exception: Exception) {
logger.error(TAG, "There was an exception while exchanging token: $exception")
null
}
}
}