I am trying to implement Google sign-in functionality for a TV, so I have been using the sign-in flow specifically designed for TVs and Devices. You can find more information about it here: https://developers.google.com/identity/sign-in/devices
While testing with Postman or curl, everything works as expected. However, when I try to do the same in my browser, I encounter a 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.
This is the code snippet I am using:
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-ins, redirection to Google's page helps avoid the CORS issue. But for TV/device sign-ins, I need to receive a code that can be displayed to the user for further action on another device.
It's worth noting that this code is within an iframe. I also tested it outside the iframe and encountered the same issue.
UPDATE I tried changing the setRequestHeader to 'r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");' and modified the send method to "r.send("client_id=&scope=email");".
- The console log shows the error message: "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 indicates Status Code = 200 (with no Response),
- However, when checking the status using r.status in the code, it returns status = 0.
Is there anyone who has successfully implemented Google sign-in for TVs/devices using JavaScript?