I need to allow a user to sign in with google on a TV, so I am using sign-in flow for TVs and Devices (https://developers.google.com/identity/sign-in/devices)
In Postman or with curl, it works fine. However, in my browser, I am getting this CORS error:
XMLHttpRequest cannot load https ://accounts.google.com/o/oauth2/device/code. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: //localhost:81' is therefore not allowed access. The response had HTTP status code 400.
Here is my code:
var data = {
client_id: '<my cliendId>',
scope: 'email'
};
var r = new XMLHttpRequest();
r.open('POST', 'https://accounts.google.com/o/oauth2/device/code', true);
r.setRequestHeader("Content-type", "application/json");
r.onreadystatechange = function () {
if (r.readyState === XMLHttpRequest.DONE) {
if (r.status === 200) {
console.log('hooray!');
}
else {
console.log('oh no');
}
}
};
r.send(JSON.stringify(data));
For web sign-in, it is necessary to redirect the user to google's page, which avoids the CORS issue. But, for TV/device sign-in, I should be getting a code back to display to the user, so that they can go to their computer and grant permission.
Incidentally, this code is in an iframe. I tested it outside of the iframe and had the same problem.
UPDATE I updated the setRequestHeader to 'r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");' and the send to "r.send("client_id=&scope=email");".
- The console log is telling me "https ://accounts.google.com/o/oauth2/device/code. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:81' is therefore not allowed access.",
- The Network tab in devtools is showing Status Code = 200 (still nothing in Response),
- But in the code where I check for r.status, I am seeing status = 0.
Does anyone see an example of using google sign-in for TVs/devices in javascript?