Could you look through my code please?
function auth(){
var url = 'https://id.twitch.tv/oauth2/token'
var data = {
"client_secret": client_secret,
"client_id": client_id,
"grant_type": "client_credentials"
};
var options = {
'method' : 'post',
'payload' : data
};
var response = UrlFetchApp.fetch(url,options);
console.log(response.getContentText())
var accesstoken = JSON.parse(response.getContentText()).access_token
createSubscription(accesstoken)
}
It gives me:
{"access_token":"XXXXXXXXXXXXXXXXX","expires_in":5000957,"token_type":"bearer"}
Then,
function createSubscription(accesstoken) {
var callback = url+"/webhooks/callback"
var data = {
"type": "channel.follow",
"version": "1",
"condition": {
"broadcaster_user_id": String(givinglifetopID)
},
"transport": {
"method": "webhook",
"callback": callback,
"secret": "some secret code"
}
};
var options = {
'method' : 'post',
'headers' : {
'Client-ID' : client_id,
'Authorization' : "Bearer "+accesstoken,
'Content-Type' : "application/json"},
'payload' : JSON.stringify(data)
};
var response = UrlFetchApp.fetch('https://api.twitch.tv/helix/eventsub/subscriptions', options);
console.log(response.getContentText())
responseToSubReq(response.getContentText(),data);
}
It gives:
{"data":[{"id":"XXXXXXXXXXXX","status":"webhook_callback_verification_pending","type":"channel.follow","version":"1","condition":{"broadcaster_user_id":"116528496"},"created_at":"2021-03-24T11:47:32.311927062Z","transport":{"method":"webhook","callback":"XXXXXXXXXXXX"},"cost":1}],"limit":10000,"total":15,"max_total_cost":10000,"total_cost":1}
Then:
function sig(data) {
var blob = Utilities.newBlob(data)
var encoded = Utilities.base64Encode(blob.getBytes())
var message = Utilities.base64Decode(encoded);
var key = Utilities.base64Decode("a2V5a2V5a2V5")
var signature = Utilities.computeHmacSha256Signature(message, key);
var sig = signature.reduce(function(str,chr){
chr = (chr < 0 ? chr + 256 : chr).toString(16);
return str + (chr.length==1?'0':'') + chr;
},'');
console.log(sig)
return sig;
}
Which gives me a signature code.
At least, I’m trying to deal with all of it by function responseToSubReq:
And here I get:
“Exception: Request failed for https://api.twitch.tv returned code 401. Truncated server response: {“error”:“Unauthorized”,“status”:401,“message”:“OAuth token is missing”}”