I'm playing around with the Ecobee API and trying to write a web page where people will go to get a PIN for my app. I have to be missing something bc nothing happens when I run the below code in CodeRunner. Can someone point out what I'm doing wrong? Obviously I'm going to want it to format this and make it look nice, but first I need to be able to retrieve the data. Thank you so much.
Mike
<HTML>
<head>
<title>Ecobee API PIN TEST</title>
</head>
<body onload="PINCode();">
<script type="text/javascript">
function PINCode() {
apiKey = 'API_KEY'
// set all apiKey inputs to have apiKey value entered
$(".apiKey").each(function() {
$(this).val(apiKey);
});
var url = 'https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=' + apiKey + '&scope=smartWrite';
$.getJSON(url, function(data) {
// set authCode to be code attribute of response
$("#authCode").val(data.code);
var response = JSON.stringify(data, null, 4);
$('#response1').html(response);
})
.error(function(jqXHR, textStatus, errorThrown) {
$('#response1').html("The following error was returned: <br><br>" + jqXHR.responseText)
});
}
</script>
<pre id="response1" class="code-json">(Response JSON will appear here...I hope.)</pre>
</body>
This is my latest attempt after Carsten's input. Now I'm getting a value to my text field, but it's an error: {"readyState":0,"status":0,"statusText":"error"}
<HTML>
<head>
<title>Ecobee API PIN TEST</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function PINCode() {
apiKey = 'API_KEY'
// set all apiKey inputs to have apiKey value entered
$(".apiKey").each(function() {
$(this).val(apiKey);
});
var url = 'https://api.ecobee.com/authorize?response_type=ecobeePin&client_id=' + apiKey + '&scope=smartWrite';
$.getJSON(url,function(data) {
// set authCode to be code attribute of response
$("#authCode").val(data.code);
alert("JSON Ran")
var response = JSON.stringify(data, null, 4);
$('#response1').html(response);
})
.fail(function(jqXHR, textStatus) {
$('#response1').html("The following error was returned: <br><br>" + JSON.stringify(jqXHR) + "<p>Please Contact <b>M2 AV Consulting</b> at <a href=mailto:support@m2avc.com?subject='EcobeePINSupport'>support@m2avc.com</a>")
console.log(textStatus);
});
})
</script>
<pre id="response1" class="code-json">(Response JSON will appear here...I hope.)</pre>
</body>
</HTML>