def process_twitch_callback_view(request):
    port = os.environ.get('PORT', 8000)
    params = request.GET
    auth_code = params['code']
    client_id = os.environ.get('TWITCH_APP_ID', None)
    client_secret = os.environ.get('TWITCH_CLIENT_SECRET', None)
    auth_r = requests.post('https://id.twitch.tv/oauth2/token', data={'client_id': client_id, 'client_secret': client_secret, 'code': auth_code, 'grant_type': 'authorization_code', 'redirect_uri': f'{settings.BASE_URL}/twitch-callback/'})
    response_data = auth_r.json()
    if settings.DEBUG_TWITCH_CALLS:
        log_request(auth_r)
    config.TWITCH_ACCESS_TOKEN = response_data['access_token']
    config.TWITCH_REFRESH_TOKEN = response_data['refresh_token']
    expiration = (timezone.now() + datetime.timedelta(seconds=response_data['expires_in']))
    config.TWITCH_ACCESS_TOKEN_EXPIRATION = expiration
    scope = ' '.join(settings.TOKEN_SCOPES)
    app_auth_r = requests.post('https://id.twitch.tv/oauth2/token', data={'client_id': client_id, 'client_secret': client_secret, 'grant_type': 'client_credentials', 'scope': scope})
    if settings.DEBUG_TWITCH_CALLS:
        log_request(app_auth_r)
    app_auth_data = app_auth_r.json()
    config.TWITCH_APP_ACCESS_TOKEN = app_auth_data['access_token']
    config.SCOPE_UPDATED_NEEDED = False
    config.SCOPES_REFRESHED = True
    headers = {'Authorization': 'Bearer {}'.format(config.TWITCH_ACCESS_TOKEN), 'Client-Id': client_id}
    user_r = requests.get('https://api.twitch.tv/helix/users', headers=headers)
    if settings.DEBUG_TWITCH_CALLS:
        log_request(user_r)
    user_data = user_r.json()
    channel_id = user_data['data'][0]['id']
    config.CHANNEL_ID = channel_id
    return HttpResponseRedirect('/')
def process_twitch_callback_view(request):
    port = os.environ.get('PORT', 8000)
    params = request.GET
    auth_code = params['code']
    client_id = os.environ.get('TWITCH_APP_ID', None)
    client_secret = os.environ.get('TWITCH_CLIENT_SECRET', None)
    auth_r = requests.post('https://id.twitch.tv/oauth2/token', data={'client_id': client_id, 'client_secret': client_secret, 'code': auth_code, 'grant_type': 'authorization_code', 'redirect_uri': f'{settings.BASE_URL}/twitch-callback/'})
    response_data = auth_r.json()
    if settings.DEBUG_TWITCH_CALLS:
        log_request(auth_r)
    config.TWITCH_ACCESS_TOKEN = response_data['access_token']
    config.TWITCH_REFRESH_TOKEN = response_data['refresh_token']
    expiration = (timezone.now() + datetime.timedelta(seconds=response_data['expires_in']))
    config.TWITCH_ACCESS_TOKEN_EXPIRATION = expiration
    scope = ' '.join(settings.TOKEN_SCOPES)
    app_auth_r = requests.post('https://id.twitch.tv/oauth2/token', data={'client_id': client_id, 'client_secret': client_secret, 'grant_type': 'client_credentials', 'scope': scope})
    if settings.DEBUG_TWITCH_CALLS:
        log_request(app_auth_r)
    app_auth_data = app_auth_r.json()
    config.TWITCH_APP_ACCESS_TOKEN = app_auth_data['access_token']
    config.SCOPE_UPDATED_NEEDED = False
    config.SCOPES_REFRESHED = True
    headers = {'Authorization': 'Bearer {}'.format(config.TWITCH_ACCESS_TOKEN), 'Client-Id': client_id}
    user_r = requests.get('https://api.twitch.tv/helix/users', headers=headers)
    if settings.DEBUG_TWITCH_CALLS:
        log_request(user_r)
    user_data = user_r.json()
    channel_id = user_data['data'][0]['id']
    config.CHANNEL_ID = channel_id
    return HttpResponseRedirect('/')