Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

twitch: use app access token #59

Merged
merged 3 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion functions/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ declare module "process" {
interface ProcessEnv {
YOUTUBE_API: string;
TWITCH_API: string;
TWITCH_TOKEN: string;
TWITCH_CLIENT_ID: string;
TWITCH_CLIENT_SECRET: string;
TWIT_CASTING_TOKEN: string;
YOUTUBE_CHANNELIDS_PATH: string;
TWITCH_CHANNELIDS_PATH: string;
Expand Down
6 changes: 3 additions & 3 deletions functions/src/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export const getVspoYoutube = onRequest(
);

export const getVspoTwitch = onRequest(
{ secrets: ["TWITCH_TOKEN", "TWITCH_CLIENT_ID"] },
{ secrets: ["TWITCH_CLIENT_ID", "TWITCH_CLIENT_SECRET"] },
async (req, res) => {
const TW_TOKEN = process.env.TWITCH_TOKEN;
const TW_CLIENT_ID = process.env.TWITCH_CLIENT_ID;
const TW_CLIENT_SECRET = process.env.TWITCH_CLIENT_SECRET;
const TW_CHANNELIDS_PATH = process.env.TWITCH_CHANNELIDS_PATH;

const twChSnap = await db.ref(TW_CHANNELIDS_PATH).get();
if (twChSnap.exists()) {
const twStreams = await twitch.getStreams(
TW_TOKEN,
TW_CLIENT_ID,
TW_CLIENT_SECRET,
twChSnap.val()
);
res.send(twStreams);
Expand Down
12 changes: 6 additions & 6 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const updateChannels = onSchedule(
schedule: "0 15 * * *",
secrets: [
"YOUTUBE_API",
"TWITCH_TOKEN",
"TWITCH_CLIENT_ID",
"TWITCH_CLIENT_SECRET",
"TWIT_CASTING_TOKEN",
],
},
Expand All @@ -40,15 +40,15 @@ export const updateChannels = onSchedule(
}

//twitch
const TW_TOKEN = process.env.TWITCH_TOKEN;
const TW_CLIENT_ID = process.env.TWITCH_CLIENT_ID;
const TW_CLIENT_SECRET = process.env.TWITCH_CLIENT_SECRET;
const TW_CHANNELIDS_PATH = process.env.TWITCH_CHANNELIDS_PATH;

const twChSnap = await db.ref(TW_CHANNELIDS_PATH).get();
if (twChSnap.exists()) {
const twChannels = await twitch.getChannels(
TW_TOKEN,
TW_CLIENT_ID,
TW_CLIENT_SECRET,
twChSnap.val()
);
db.ref(`${DATA_URI}/twitch/channels`).set(twChannels);
Expand Down Expand Up @@ -103,20 +103,20 @@ export const updateYoutubeStreams = onSchedule(
export const updateTwitchAndTwitCastingStreams = onSchedule(
{
schedule: "1,11,21,31,41,51 * * * *",
secrets: ["TWITCH_TOKEN", "TWITCH_CLIENT_ID", "TWIT_CASTING_TOKEN"],
secrets: ["TWITCH_CLIENT_ID", "TWITCH_CLIENT_SECRET", "TWIT_CASTING_TOKEN"],
},
async (_) => {
const DATA_URI = process.env.DATA_URI;

const TW_TOKEN = process.env.TWITCH_TOKEN;
const TW_CLIENT_ID = process.env.TWITCH_CLIENT_ID;
const TW_CLIENT_SECRET = process.env.TWITCH_CLIENT_SECRET;
const TW_CHANNELIDS_PATH = process.env.TWITCH_CHANNELIDS_PATH;

const twChSnap = await db.ref(TW_CHANNELIDS_PATH).get();
if (twChSnap.exists()) {
const twStreams = await twitch.getStreams(
TW_TOKEN,
TW_CLIENT_ID,
TW_CLIENT_SECRET,
twChSnap.val()
);
db.ref(`${DATA_URI}/twitch/streams`).set(twStreams);
Expand Down
35 changes: 26 additions & 9 deletions functions/src/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
import axios from "axios";
import { ChannelInfo, StreamInfo } from "./types";

const authURL = "https://id.twitch.tv/oauth2/token";
const baseURL = "https://api.twitch.tv/helix";

const doGetRequestTwitch = async (
url: string,
token: string,
clientId: string
clientId: string,
clientSecret: string
) => {
try {
// get app access token
// https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#client-credentials-grant-flow
const authRes = await axios.post(
authURL,
{
client_id: clientId,
client_secret: clientSecret,
grant_type: "client_credentials",
},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);

const res = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`,
Authorization: `Bearer ${authRes.data.access_token}`,
"Client-Id": clientId,
"Content-Type": "application/json",
},
Expand All @@ -24,8 +41,8 @@ const doGetRequestTwitch = async (
};

export const getChannels = async (
token: string,
clientId: string,
clientSecret: string,
channelIds: string[]
) => {
const url = `${baseURL}/users?${channelIds
Expand All @@ -34,8 +51,8 @@ export const getChannels = async (

const contents: { data: any[] } = await doGetRequestTwitch(
url,
token,
clientId
clientId,
clientSecret
);

return contents.data.map(
Expand All @@ -48,8 +65,8 @@ export const getChannels = async (
};

export const getStreams = async (
token: string,
clientId: string,
clientSecret: string,
channelIds: string[]
) => {
const url = `${baseURL}/streams?first=50&${channelIds
Expand All @@ -58,8 +75,8 @@ export const getStreams = async (

const contents: { data: any[] } = await doGetRequestTwitch(
url,
token,
clientId
clientId,
clientSecret,
);

return contents.data.map(
Expand Down