I am having problems trying to use the Wordnik API via Swagger (NodeJS).
I am trying to follow this documentation: swagger-api
The Wordnik API can be founded here: hhttp://developer.wordnik.com/docs.html#!/word/getRelatedWords_get_4
The JSON description of the Wordnik API: hhttp://developer.wordnik.com/v4/word.json
I am trying to call the GET /word.json/{word}/relatedWords method with the following parameters:
{word:"cars", useCanonical:true, relationshipTypes:"synonim", limitPerRelationshipType:10}
The Wordnik API requires authentification, according to the swagger-api documentation I have written the following code:
var Swagger = require('swagger-client');
var client = new Swagger({
url: 'http://developer.wordnik.com/v4/word.json',
success: function() {
client.word.getRelatedWords({word:"cars",useCanonical:true,relationshipTypes:"synonim",limitPerRelationshipType:10}, function(success){
console.log('succeeded and returned this object: ' + success.obj);
},
function(error) {
console.log('failed with the following: ' + error.statusText);
});
},
authorizations: {
api_key: new Swagger.ApiKeyAuthorization('api_key', 'MY_WORDNIK_API_KEY', 'query'),
}
});
My main problem is that I do not know how to write properly the code to call that method with that parameters. The code written above returns this error:
failed with the following: {"message": "unauthorized", "type": "error"}
Any idea about how to write the code to make that call?
Two things: one, the Swagger url for Wordnik should be
http://api.wordnik.com/v4/word.json
Two, you need to use 'synonym', not 'synonim'.
I tried your code with both these changes and it works fine for me.
Also, I'm not sure why you're using Swagger.js here? It's definitely cooler, but you can call the API with request, e.g.:
request("http://api.wordnik.com:80/v4/word.json/"+'car'+"/relatedWords?useCanonical=false&relationshipTypes=synonym&limitPerRelationshipType=10&api_key=YOURKEYHERE", function (error, response, body) {
if (error || response.statusCode !== 200 || (body == "")) {
console.log(word + " had error: " + error)
return callback(error || {statusCode: response.statusCode});
}
else {
// console.log(word);
callback(null, JSON.parse(body));
}
});