The items you linked to helped a lot. But I am still having some trouble getting everything working properly.
I have been able to perform the call correctly, but in ordfer to streamline my code I decided to functionize the call.
My major is that my function “APICall” just refuses to ‘return’ data (either the JSON object or a simple string. Neither is returning to parent call.). I verified this by having the 'document.write(“Response Received”) line in the parent routine ‘CheckStreamerOnline’.
I have been testing my code on : https://js.do/ for quick and easy testing, so all of the ‘document.write’ stuff is to get output there. (You can copy-paste this write into that page to get a live result)
Here is my code:
<script>
const oAuthorizationURL = 'https://id.twitch.tv/oauth2/authorize?' ;
const ImplicitAuthURL = 'https://api.twitch.tv/helix/users?' ;
const TokenValidationURL = 'https://api.twitch.tv/helix/'
//'https://id.twitch.tv/oauth2/validate'
const TokenURL = 'https://id.twitch.tv/oauth2/token?' ;
const StreamsURL = 'https://api.twitch.tv/helix/streams?'
const AppRedirectURL = 'http://localhost'
const AppClientID = 'y4344ir7sj3tem9cqnzkv288grm4e9'
var ApiToken = 'REMOVED'
async function APICall(URL, HeadersObject) {
// Perform API Call and return a JSON object
document.write("<br> Performing API Call to URL: " + URL)
fetch( URL , { headers: HeadersObject } )
.then(resp => resp.json())
.then(resp => {
if (resp.error) {
document.write("<br> URL: " + URL)
document.write("<br>")
document.write("<br>API Response:")
document.write("<br>")
document.write("Error: " + resp.error)
document.write("<br>")
document.write("Status: " + resp.status)
document.write("<br>")
document.write("Message: " + resp.message)
document.write("<br>")
return "ERROR"
}else {
document.write("<br>API Returned without error <br>")
document.write("<br><br> Literal Reponse: <br>")
document.write(JSON.stringify(resp) + "<br><br>")
return resp.json()
}
})
.catch(resp,err => {
document.write('🤬 API Access Error: ', err);
try {
document.write("<br><br> Literal Reponse: <br>")
document.write(JSON.stringify(resp) + "<br><br>")
}
finally{ return ERROR };
});
return "ERROR"
};
async function CheckStreamerOnline(ChannelURL) {
var bolONLINE = false;
if (ChannelURL === 'NoValueSet' || ChannelURL === '' || ChannelURL == null ) {
// No Value -> Ignore
bolOnline = false;
} else {
//Perform API check to determine if the specific channel is online
var myHeaders = new Headers(); // Currently empty
myHeaders.append("Client-ID", AppClientID)
myHeaders.append("Authorization", "Bearer " + ApiToken)
var Resp = await APICall(StreamsURL + 'user_login=' + ChannelURL, myHeaders);
document.write("<br>Response Received<br>")
if (Resp.data[0]){
bolONLINE = TRUE
}else {
bolONLINE = FALSE
};
}; // end of large if statement
return bolONLINE
};
async function main() {
document.write("<br>" +"=========================");
var R = await CheckStreamerOnline('warframe');
document.write("<br>CheckStreamerOnline Response: <br>" + R);
};
main();
</script>