This would basically be a javascript function that you define in the main page.
This function does not exist: you will have to create it and make it do something.
Yeah, I understand that. What I’m trying to figure out is what that function needs to do with the key in order for the SDK to have access to it on the main page. It’s possible that nothing normally needs to be done and my confusion is due to having misimplemented some aspect of the flow.
JB940 is correct with regards to that Stack Overflow post. Also, have you tried the getToken function3 in the JavaScript SDK to see if it returns anything?
getToken returns ‘undefined’, which is my problem. It seems like the assumption in the Stack Overflow post is that the Twitch SDK reference on the main page is somehow supposed to have access to the token, even though it’s the SDK reference on the redirect that actually received it. Am I understanding that correctly?
Here’s my code that actually logs in to Twitch, which is sitting in my index.html file:
Twitch.login({
redirect_uri: 'http://mydomain.com/oauth_redirect.html',
scope: ['user_read', 'channel_read'],
popup: true
});
And then on the oauth_redirect.html I have the following code:
$(function() {
if (window.location.hash.indexOf('access_token') != -1) {
var accessToken = window.location.hash.replace("#access_token=", "");
accessToken = accessToken.substr(0, accessToken.indexOf('&scope'));
window.opener.setAuthToken(accessToken);
window.close();
} else {
// This is executed if someone blindly accesses the page.
alert("no token");
}
});
Both index.html and oauth_redirect.html have references to the Twitch SDK, like so:
<script src="https://ttv-api.s3.amazonaws.com/twitch.min.js"></script>
After the redirect page closes itself, however, my main page does not have a valid access token.