{"id":13338765,"url":"https://github.com/InterfaceGUI/Twitch-API-Token-Refresher","last_synced_at":"2025-03-11T10:31:54.450Z","repository":{"id":158528821,"uuid":"626181936","full_name":"InterfaceGUI/Twitch-API-Token-Refresher","owner":"InterfaceGUI","description":"This is a Python program based on the Flask architecture for refreshing the Twitch API tokens.","archived":false,"fork":false,"pushed_at":"2023-04-11T05:40:04.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-10-23T22:19:05.232Z","etag":null,"topics":["flask","python3","token-refresh","twitch","twitch-api"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/InterfaceGUI.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-11T00:55:08.000Z","updated_at":"2024-08-31T04:53:03.000Z","dependencies_parsed_at":"2023-08-21T19:30:58.397Z","dependency_job_id":null,"html_url":"https://github.com/InterfaceGUI/Twitch-API-Token-Refresher","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InterfaceGUI%2FTwitch-API-Token-Refresher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InterfaceGUI%2FTwitch-API-Token-Refresher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InterfaceGUI%2FTwitch-API-Token-Refresher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/InterfaceGUI%2FTwitch-API-Token-Refresher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/InterfaceGUI","download_url":"https://codeload.github.com/InterfaceGUI/Twitch-API-Token-Refresher/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243015443,"owners_count":20222082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["flask","python3","token-refresh","twitch","twitch-api"],"created_at":"2024-07-29T19:17:21.745Z","updated_at":"2025-03-11T10:31:54.439Z","avatar_url":"https://github.com/InterfaceGUI.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Twtich-API-Token-Refresher\nThis is a Python program based on the Flask architecture for refreshing the Twitch API tokens.\n\n## install \u0026 Setup\n\n### You need to create the Twitch application first\n\n1. Go to the [Twitch Developer Console](https://dev.twitch.tv/console/apps) and create a new application\n\n1. OAuth Redirect URL fill in: `https://localhost/twitch-auth-callback`\n\n1. Write down the `Client ID` and `Client Secret` will be used later.\n\n\u003cbr\u003e\n\n### Docker\n\nThe ssl folder must be mounted to mount, and The certificate must be named as follows:\n\n* `server.crt`\n\n* `server.key`\n\nExample using docker-compose:\n\n```yml\nversion: \"3.9\"\n\nservices:\n  TwitchAPI:\n    image: \"ghcr.io/interfacegui/twitch-api-token-refresher:latest\"\n    environment:\n      ClientID: \"YOUR Twitch APP ID\"\n      ClientSecret: \"YOUR Twitch APP SECRET\"\n      scope: \"channel:read:redemptions,user:read:email,channel:read:subscriptions\"\n    volumes:\n      - /home/ubuntu/ssl:/app/ssl\n    ports:\n      - \"3000:3000\"\n```\n\n## How to use\n\n* Go to `/login`endpoint to Get `Access Token` and `Refresh Token`\n\n* Refresh the token using the `/refresh` endpoint:\n\u003cdetails\u003e \u003csummary\u003eRefresh Token\u003c/summary\u003e\n\n```javascript\n  await fetch('https://0.0.0.0:3000/refresh',{\n      method: \"POST\", headers: {'Origin':'*','content-type': 'application/json'},\n      body:JSON.stringify({\"refresh_token\": 'RefreshToken'})\n  })\n  .then(function(response) {\n    return response.json();\n  })\n  .then(function(myJson) {\n    console.log(myJson);\n  });\n\n```\n\u003c/details\u003e\n\n## Example for the streamelement widget\n\u003cdetails\u003e \u003csummary\u003eValidate and refresh the token\u003c/summary\u003e\n\n```javascript\nwindow.addEventListener('onWidgetLoad', async function (obj) {\n    await TwitchAPI_validate()\n});\n```\n\n```javascript\nconst twitchOAuthToken = \"{{Twitch_access_token}}\"\nconst TwitchRefreshToken = \"{{Twitch_refresh_token}}\"\n\nasync function TwitchAPI_validate(){\n    \n    //Test the access token. If the token expires, twitch will return 401.\n    const response = await fetch('https://id.twitch.tv/oauth2/validate',{\n        method: \"GET\",\n        headers: {\n            Authorization: `Bearer ${twitchOAuthToken}`\n        }\n    })\n    \n    if ( response.status == 401){\n        console.log('invalid access token')\n        console.log('Getting new token...')\n        \n        const newtoken = await fetch('https://0.0.0.0:3000/refresh',{\n            method: \"POST\", headers: {'Origin':'*','content-type': 'application/json'},\n            body:JSON.stringify({\"refresh_token\":TwitchRefreshToken})\n        })\n\n        if ( newtoken.status != 200){return false}\n        \n      \tlet respData = await newtoken.json();\n        console.log(respData);\n        \n        //Update widget Field\n        SE_API.setField('Twitch_access_token', respData['access_token']);\n    }\n    return true\n}\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e \u003csummary\u003eUse the twitch api\u003c/summary\u003e\n\n```javascript\nawait fetch('https://api.twitch.tv/helix/users',{\n    method: \"GET\",\n    headers: {\n        Authorization: `Bearer ${twitchOAuthToken}`,\n        'Client-Id': '---Client id here---'\n    }\n})\n.then((response) =\u003e {\n    return response.json()\n})\n.catch((error) =\u003e {\n    console.log(`Error: ${error}`);\n})\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e \u003csummary\u003efields.json\u003c/summary\u003e\n\n```json\n{\n    \"Twitch_access_token\": {\n        \"type\": \"text\",\n        \"label\": \"Twitch Access Token\",\n        \"value\": \"\",\n        \"group\": \"Twitch Settings\"\n    },\n    \"Twitch_refresh_token\": {\n        \"type\": \"text\",\n        \"label\": \"Twitch Refresh Token\",\n        \"value\": \"\",\n        \"group\": \"Twitch Settings\"\n    }\n}\n```\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInterfaceGUI%2FTwitch-API-Token-Refresher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FInterfaceGUI%2FTwitch-API-Token-Refresher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FInterfaceGUI%2FTwitch-API-Token-Refresher/lists"}