From ec7db687122dd47b218b1c0e90d85a4a1d4f3234 Mon Sep 17 00:00:00 2001 From: madewithkode Date: Fri, 10 Jan 2025 13:26:08 +0100 Subject: [PATCH] add bodyserializer/accept header to openverse api client to correctly authenticate with the API --- packages/js/api-client/src/auth.ts | 6 ++++++ packages/js/api-client/tests/client.test.ts | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/js/api-client/src/auth.ts b/packages/js/api-client/src/auth.ts index 95b8ccd51af..278899be4c3 100644 --- a/packages/js/api-client/src/auth.ts +++ b/packages/js/api-client/src/auth.ts @@ -136,6 +136,12 @@ export class OpenverseAuthMiddleware implements OpenverseMiddleware { client_id: this.credentials.clientId, client_secret: this.credentials.clientSecret, }, + bodySerializer(body) { + return new URLSearchParams(body).toString() + }, + headers: { + Accept: "x-www-form-encoded", + }, }) .then((tokenResponse) => { if (!tokenResponse.response.ok || !tokenResponse.data) { diff --git a/packages/js/api-client/tests/client.test.ts b/packages/js/api-client/tests/client.test.ts index 7e648207d03..1e21d81086a 100644 --- a/packages/js/api-client/tests/client.test.ts +++ b/packages/js/api-client/tests/client.test.ts @@ -26,7 +26,15 @@ describe("OpenverseClient", () => { }) const scope = nock - .post("/v1/auth_tokens/token/", /test-secret/) + .post("/v1/auth_tokens/token/", (body) => { + const params = new URLSearchParams(body) + + // Check if the required parameter matches + return ( + params.get("client_id") === "test" && + params.get("client_secret") === "test-secret" + ) + }) .reply(200, { access_token: "test-access-token", scope: "test-scope",