Skip to content

Commit

Permalink
add bodyserializer/accept header to openverse api client to correctly…
Browse files Browse the repository at this point in the history
… authenticate with the API
  • Loading branch information
madewithkode committed Jan 12, 2025
1 parent 363166e commit ec7db68
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/js/api-client/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion packages/js/api-client/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit ec7db68

Please sign in to comment.