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

Add bodySerializer and Accept header to @openverse/api-client to correctly authenticate with the API #5314

Open
obulat opened this issue Jan 1, 2025 · 0 comments
Assignees
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix good first issue New-contributor friendly help wanted Open to participation from the community 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: frontend Related to the Nuxt frontend

Comments

@obulat
Copy link
Contributor

obulat commented Jan 1, 2025

Description

The @openverse/api-client package sends the requests for token to the Openverse Django API with the correct parameters but does not serialize the body to the required x-www-form-urlencoded format. This causes the requests to be sent with an empty body.

Solution

Add bodySerializer1 that creates new URLSearchParams from the body object, and stringifies it:

body: {
...
},
bodySerializer(body) {
    return new URLSearchParams(body).toString()
},
headers: {
   "Accept": "x-www-form-encoded"
}
...

this.client
.POST("/v1/auth_tokens/token/", {
body: {
grant_type: "client_credentials",
client_id: this.credentials.clientId,
client_secret: this.credentials.clientSecret,
},

Tests update

The matching of the request in tests should also be updated to ensure we always get the correct encoding.

Update this line:

.post("/v1/auth_tokens/token/", /test-secret/)

With this code:

  .post('/v1/auth_tokens/token/', (body) => {
    cconst params = new URLSearchParams(body);

    // Check if the required parameter matches
    return params.get('clientId') === 'test' && params.get("clientSecret") === "test-secret" 
  })

Additional context

I opened an issue in the openapi-fetch repository to encode the body automatically when the relevant header is passed: Automatically encode the body when "Accept": "application/x-www-form-encoded" header is passed

This issue was identified when I was trying to use @openverse/api-client in the "Proxy frontend API requests" project planning.

Footnotes

  1. Documentation on bodySerializer in openapi-fetch

@obulat obulat added good first issue New-contributor friendly help wanted Open to participation from the community 💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: frontend Related to the Nuxt frontend labels Jan 1, 2025
@openverse-bot openverse-bot moved this to 📋 Backlog in Openverse Backlog Jan 1, 2025
@madewithkode madewithkode self-assigned this Jan 6, 2025
@openverse-bot openverse-bot moved this from 📋 Backlog to 📅 To Do in Openverse Backlog Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix good first issue New-contributor friendly help wanted Open to participation from the community 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: frontend Related to the Nuxt frontend
Projects
Status: 📅 To Do
Development

No branches or pull requests

2 participants