Skip to content

Commit

Permalink
Fix Exchanging Token always returns 401 (#178)
Browse files Browse the repository at this point in the history
# Description
The API doesn't like the trailing "charset=utf-8" in the content-type
header. Removing it allows the calls to process properly for auth.

# License
<!-- Your PR comment must contain the following line for us to merge the
PR. -->
I confirm that this contribution is made under the terms of the MIT
license and that I have the authority necessary to make this
contribution on behalf of its copyright owner.
  • Loading branch information
mrashed-dev committed Dec 14, 2023
1 parent 12b63cd commit 063f385
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
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())
}
}
}

0 comments on commit 063f385

Please sign in to comment.