Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Exchanging Token always returns 401 #178

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Nylas Java SDK Changelog

## [2.0.0-beta.3] - TBD
* Fixed `Auth.exchangeCodeForToken` always returning 401

## [2.0.0-beta.2] - Released 2023-11-21

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@ class ContentHeadersInterceptor : Interceptor {
val path = request.url().encodedPath()
val contentHeader = request.header(NylasClient.HttpHeaders.CONTENT_TYPE.headerName)
if (contentHeader == null && !isDownloadablePath(path)) {
val enhancedRequest = request
.newBuilder()
.header(
val enhancedRequest = request.newBuilder()
if (request.body() != null && request.body()!!.contentType() != null) {
enhancedRequest.header(
NylasClient.HttpHeaders.CONTENT_TYPE.headerName,
request.body()!!.contentType()!!.toString(),
)
} else if (request.body() != null) {
enhancedRequest.header(
NylasClient.HttpHeaders.CONTENT_TYPE.headerName,
NylasClient.MediaType.APPLICATION_JSON.mediaType,
)
.header(NylasClient.HttpHeaders.ACCEPT.headerName, NylasClient.MediaType.APPLICATION_JSON.mediaType)
.build()
return chain.proceed(enhancedRequest)
}

enhancedRequest.header(NylasClient.HttpHeaders.ACCEPT.headerName, NylasClient.MediaType.APPLICATION_JSON.mediaType)
return chain.proceed(enhancedRequest.build())
}
return chain.proceed(request)
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/com/nylas/models/NylasOAuthError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ data class NylasOAuthError(
*/
@Json(name = "error_code")
var errorCode: String,
/**
* The HTTP status code of the error response.
*/
@Json(name = "request_id")
override var requestId: String? = null,
/**
* The HTTP status code of the error response.
*/
override var statusCode: Int? = null,
) : AbstractNylasApiError(error, statusCode)
) : AbstractNylasApiError(error, statusCode, requestId)
4 changes: 2 additions & 2 deletions src/main/kotlin/com/nylas/util/JsonHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ class JsonHelper {
return json
}

private val jsonType = MediaType.parse("application/json; charset=utf-8")
private val jsonType = MediaType.parse("application/json")

/**
* Get the JSON media type.
Expand Down Expand Up @@ -225,7 +225,7 @@ class JsonHelper {
*/
@JvmStatic
fun jsonRequestBody(json: String): RequestBody {
return RequestBody.create(jsonType(), json)
return RequestBody.create(jsonType(), json.toByteArray())
}
}
}