Forbidden request status while using oxford dictionary API in javascript - javascript

I am learning to use web apis. I started with oxford dictionary api and read this documentation. This is the code I have written so far.
const app_id = '8***c965';
const app_key = '395******162b317b9d6f3c6cd0b3930b';
const language = "en";
var word_id = 'oblivion';
var url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/' + language + '/' + word_id.toLowerCase();
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.setRequestHeader('app_id', app_id);
request.setRequestHeader('app_key', app_key);
request.setRequestHeader('Accept', "application/json");
request.onload = function() {
//Begin accessing JSON data here
var data = JSON.parse(this.response);
console.log(data);
}
request.send();
This code returns status 403 Forbidden in the console. According to the documentation, this should have worked. Can anybody shed some light about what I am doing wrong? Any help is greatly appreciated!

You have exceeded a monthly limit. The limit is 3k per month.
You can see it in "statistic" tab on https://developer.oxforddictionaries.com.
For development it is better to use some mock server in order to save hits for real application.

If none of what mentioned above fixed the issue this could be it.
Their API version 1 got shut down on 30 June 2019 and replaced by V2.
From their documentation:
Users on a FREE account will need to update their plan to either
Prototype or Developer to get access to v2. More information about our
new plans is available here.
Use 'v1' in the URL to request version 1, and 'v2' to request version
2.
Read about migration guide here: https://developer.oxforddictionaries.com/version2

Related

Why does restcountries API does not work while my Javascript code is correct

I'm trying to get data about countries using Rest Countries API by using link which is inside Github repository https://github.com/public-apis/public-apis.
And when I open this API This is what I get.
{"message":"Page Not Found","_links":{"self":{"href":"/","templated":false}}}.
And this is my Javascript Code
const getCountryData = function (country) {
const request = new XMLHttpRequest();
request.open('GET', `https://restcountries.eu/rest/v2/name/${country}`);
request.send();
request.addEventListener('load', function () {
console.log(this.responseText);
});
};
getCountryData('rwanda');
getCountryData('brazil');
What is this happening ???
when I try to open the restcountries.eu site it redirects me to this http://countrylayer.com/ site maybe they have changed the domain or if you cannot figure it out then you can check out this site and see if it help you to do what you want to :
https://github.com/samayo/country-json

Difference between Electron-based XMLHttpRequest and browser-based URL with query string?

I'm working on an Electron app and trying to integrate the Easy Digital Downloads Software Licensing WordPress plugin. I haven't done much with HTTP communication in Electron/Javascript so this may be a naive question.
The problem: I am able to get a license activation response from my EDD server and while there is no specific error, for some reason a license is not activated. The odd thing is that if I use a URL and query string in a browser with the same data, the plugin responds as expected: I can activate, deactivate and check the status of a license.
So EDD seems to be working and there are no errors with Electron. But something is missing. Initially I was using the net Electron module but after this issue came up, I switched to using the example script from EDD (below) which uses XMLHttpRequest. With that I get the following response back:
{"success":true,"license":"valid","item_id":539,"item_name":"My
Awesome App","license_limit":1,"site_count":0,"expires":"2020-12-19
23:59:59","activations_left":1,"checksum":"f2d66c6844b37d1fa931b813c408",
"payment_id":248,"customer_name":"Marvin
Gardens","customer_email":"marvin#home.com","price_id":false}
Which is fine except that "activations_left":1 never changes and it should given "license_limit":1. So something is wrong.
On the other hand, if I use a URL with a query string in a browser, the "activations_left" is decremented and license activation only works once (as it should). For example, this works:
http://YOURSITE.com/?edd_action=activate_license&item_id=8&license=cc22c1ec86304b36883440e2e84cddff&url=http://licensedsite.com
My Question: is there some fundamental difference between these two methods? Is there something I need to add to my XMLHttpRequest? I have a support ticket open with EDD but I need to keep moving with this. And sorry to be so long-winded!
UPDATE:
#aw04 suggested I try using GET – just tried that and I "get" the same response as before: no error but also no activation.
Could there be some property which should (or shouldn't) be in the Electron request which is (or isn't) in a browser request by default?
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log('xhttp.responseText', xhttp.responseText);
}
}
var url = "http://YOURSITE.com/?edd_action=activate_license&item_id=8&license=cc22c1ec86304b36883440e2e84cddff"
xhttp.open("GET", url);
xhttp.send();
var xhttp = new XMLHttpRequest();
var postUrl = 'http://<domain.com>/edd-sl/';
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
console.log(xhttp.responseText);
}
}
var data = {
edd_action: 'check_license',
license: '<license key>',
item_name: encodeURIComponent('<item name>'),
};
xhttp.open("POST", postUrl, true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.setRequestHeader("Access-Control-Allow-Origin", "http://local.dev");
var values = '';
for (var key in data){
values += key + '=' + data[ key ] + '&';
}
values = values.substring(0, values.length - 1);
xhttp.send(values);
Based on some help from Easy Digital Downloads support folks, this is resolved.
The issue had to do with a property in their Software Licensing plugin setup: "Do not check URL". I hadn't enabled that with the result that my API call from Electron failed and the one using a browser succeeded because the browser was adding headers that Electron was not.
After enabling "Do not check URL", calls from within Electron work. I guess there is also an option to pass in a URL, but since I am using EDD for licensing desktop software, that didn't seem like a needed option.
Anyway, hope this helps someone.

Arcgis: authentification via JS application

I am writing a program using JavaScript, which connects to a local ArcGIS server through ArcGIS REST API and loads the maps.
The URL to get service is
let xmlhttp = new XMLHttpRequest();
xmlhttp.withCredentials = true;
let url = "http://domain/arcgis/rest/services/" + serviceName +"/MapServer/" + layer_id + "/query?f=json&where=1%3D1&returnGeometry=false&outFields=*";
I can get the token through the API. The problem is, I can't set it in cookies, as the browser rejects programmatically writing another domain.
May someone suggest a workaround?
You can add &token=abcd1234 to your url, so you get :
let token = 'abcd1234';
let url = `http://domain/arcgis/rest/services/${serviceName}/MapServer/${layer_id}/query?f=json&where=1%3D1&returnGeometry=false&outFields=*&token=${token}`;

REST API call gives response code 0

I'm doing an API call to the Insightly API using this code:
var x = new XMLHttpRequest();
x.open("GET", "https://api.insight.ly/v2.2/Projects/Search?status=in%20progress&brief=false&count_total=false", true);
x.setRequestHeader('Authorization', 'Basic: 'some-basic-key-and-user');
x.send();
console.log(x.status);
but the responsecode output equals 0
I've tried my URL and AuthKey using hurl.it's tool, and it gives back exactly the response I need. I've also tried x.statusText and x.Response
How come my script doesn't give me the output I need?
EDIT:
After trying adding those parameters from Mozilla's documentation (thank you Amy). I still don't get the correct response code.
My code now:
var x = new XMLHttpRequest();
x.open("GET", "https://api.insight.ly/v2.2/Projects/Search?status=in%20progress&brief=false&count_total=false", true);
x.setRequestHeader('Authorization', 'Basic: b4c6c660-e20f-4f31-b90d-b6a11bfe4ef2');
x.onprogress = function () {
console.log('LOADING', x.status);
};
x.onload = function () {
console.log('DONE', x.status);
};
x.send();
console.log(x.status);
The response code:
0
LOADING 0
Also I've read through the possible answer, and I still don't know how to fix my mistake, I know why it is asynchronous and why it doesn't work this way, but I don't know how to fix.
First of all, you have unnecessary quote symbol on row 3 (right after some-basic-key-and-user).
Second is that insight api doesn't support cross origin resource sharing (CORS for short).
You might want to introduce some proxy on the same domain you query data from.

Calling API from CouchDB Design Doc

I am wondering if it is possible to make an API call from within a design doc. I have tried the code below, however I am getting the following error message.
{"error":"forbidden","reason":"CSRF Cookie/Header mismatch"}
This is the code:
function(head, req) {
var id = req.query.id;
var contactName = 'This is the new contact name!!';
var sendString = '{"PrimaryContactName":"' + contactName + '"}';
var xhr = new XMLHttpRequest();
xhr.open('PUT", <URL>, false);
xhr.send(sendString);
var sendStatus = xhr.status;
}
Thanks!
You aren't going to be able to use AJAX from CouchDB. (it's not a web browser)
If you want changes in your database to be propagated to other data-sources, you can use the _changes feed. That will be a much more robust solution no matter how you slice it.

Categories