📅  最后修改于: 2023-12-03 15:13:26.946000             🧑  作者: Mango
The API Streamelements Watchtime is a tool that allows Twitch streamers to keep track of how much time a viewer has spent watching their stream. This can be useful for rewarding loyal viewers, setting up giveaways, or just tracking viewer engagement.
To use the API Streamelements Watchtime in Javascript, you'll need to first authenticate with the Streamelements API. This involves obtaining an access token by following the OAuth2 process outlined in the Streamelements API documentation.
Once authenticated, you can use the GET /points/{channelId}/{username}
endpoint to retrieve a viewer's watchtime. This endpoint returns a JSON object containing the viewer's username, channel ID, and watchtime in seconds.
Here's an example implementation in Javascript:
const fetch = require('node-fetch');
async function getWatchtime(channelId, username, authToken) {
const response = await fetch(`https://api.streamelements.com/kappa/v2/points/${channelId}/${username}`, {
headers: {
Authorization: `Bearer ${authToken}`
}
});
const data = await response.json();
return data.pointsData.watchTimeSeconds;
}
This function takes in the channel ID, viewer username, and authentication token as parameters. It then uses the node-fetch
package to make a GET request to the API endpoint with the appropriate headers, and returns the watchtime in seconds.
To use this function in your application, you'll need to call it with the appropriate parameters and handle the returned watchtime value as needed.
Overall, the API Streamelements Watchtime is a powerful tool for Twitch streamers to track viewer engagement and reward loyal viewers. By using Javascript to interact with the API, you can easily incorporate watchtime tracking into your application and provide a more immersive viewing experience for your audience.