404-Fehler beim Verwenden des DiscourseAPI-Gems zur Generierung eines Benutzer-API-Schlüssels

Hallo,

ich habe Schwierigkeiten, eine Lösung für dieses Problem zu finden, und hoffe, dass jemand hier einen Hinweis hat. Kürzlich bin ich zu einem neuen Projekt gekommen und habe die Arbeit eines früheren Entwicklers übernommen. Ich habe kürzlich den Discourse-Build auf die neueste Version aktualisiert; er war einige Monate veraltet, und alle anderen Funktionen und API-Aufrufe funktionieren wie erwartet.

Mein Ziel ist es, über die API ein Thema in Discourse von unserer Website außerhalb von Discourse zu erstellen. Ich nehme die Benutzer-ID des Nutzers und suche ihn in Discourse mithilfe der Methode DiscourseAPI by_external_id. Sobald ich das habe, rufe ich generate_user_api_key(discourse_user[‘id’]) auf, wobei die ID des Benutzers in Discourse übergeben wird.

Daraufhin erhalte ich eine DiscourseApi::NotFoundError / 404-Fehlermeldung, die erst nach dem Upgrade aufgetreten ist.

Jede Hilfe oder Anleitung dazu wäre sehr willkommen!

2 „Gefällt mir“

The route for generating API keys has changed, but the method in the Discourse API gem that’s used for generating an API key is still calling the old route. That’s why you are getting the 404 error.

We will get this fixed. For now, if you would like to generate an API key for a user, you can do it by making a POST request to http://localhost:3000/admin/api/keys". Here’s an example CURL request. I’ve substituted my actual API key with $api_key in the example:

curl -X POST "http://localhost:3000/admin/api/keys" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: $api_key" \
-H "Api-Username: system" \
-F "key[description]=a key for bobby" \
-F "key[username]=bobby"

You do not need to generate a user API key for this. Instead, you can set client.api_username to the username you want to use to publish the post:

client.api_username = "susan"
client.create_topic(category: 5, title: "Susan's really awesome API topic", skip_validation: true, raw: "this is a test, this is only a test")

client.api_username = "bobby"
client.create_topic(category: 5, title: "Bobby's really awesome API topic", skip_validation: true, raw: "this is a test, this is only a test")

This will work correctly as long as you used the All Users API key when you created the client.

3 „Gefällt mir“

Thank you!

I updated my code to use the “All Users” key instead of generating one for each user and pass the user’s username as the api_username and that fixed it.

2 „Gefällt mir“

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.