# Buggy code
def check_token_access(token: str) -> None:
    if token.startswith("xoxp-"):
        logging.info(
            "This is a Slack user token, which grants all rights the user has!"
        )
    elif token.startswith("xoxb-"):
        data = requests.get("https://slack.com/api/team.info", {"token": token})
        has_scopes = set(data.headers.get("x-oauth-scopes", "").split(","))
        required_scopes = set(
            ["emoji:read", "users:read", "users:read.email", "team:read"]
        )
        missing_scopes = required_scopes - has_scopes
        if missing_scopes:
            raise ValueError(
                "Slack token is missing the following required scopes: {}".format(
                    sorted(missing_scopes)
                )
            )
    else:
        raise Exception("Unknown token type -- must start with xoxb- or xoxp-")


# Traceback (Exception)
replace slack_data/overwatch/2018-09-08.json? [y]es, [n]o, [A]ll, [N]one, [r]ename: A
Traceback (most recent call last):
File "./manage.py", line 50, in <module>
execute_from_command_line(sys.argv)
File "/home/zulip/deployments/2020-12-01-22-47-56/zulip-py3-venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/zulip/deployments/2020-12-01-22-47-56/zulip-py3-venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/zulip/deployments/2020-12-01-22-47-56/zulip-py3-venv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/zulip/deployments/2020-12-01-22-47-56/zulip-py3-venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/zulip/deployments/2020-12-01-22-47-56/zerver/management/commands/convert_slack_data.py", line 55, in handle
do_convert_data(path, output_dir, token, threads=num_threads)
File "/home/zulip/deployments/2020-12-01-22-47-56/zerver/data_import/slack.py", line 1072, in do_convert_data
user_list = get_slack_api_data("https://slack.com/api/users.list", "members", token=token)
File "/home/zulip/deployments/2020-12-01-22-47-56/zerver/data_import/slack.py", line 1142, in get_slack_api_data
raise Exception('Error accessing Slack API: {}'.format(result['error']))
Exception: Error accessing Slack API: invalid_auth

# Fixed code
def check_token_access(token: str) -> None:
    if token.startswith("xoxp-"):
        logging.info(
            "This is a Slack user token, which grants all rights the user has!"
        )
    elif token.startswith("xoxb-"):
        data = requests.get(
            "https://slack.com/api/team.info",
            headers={"Authorization": "Bearer {}".format(token)},
        )
        has_scopes = set(data.headers.get("x-oauth-scopes", "").split(","))
        required_scopes = set(
            ["emoji:read", "users:read", "users:read.email", "team:read"]
        )
        missing_scopes = required_scopes - has_scopes
        if missing_scopes:
            raise ValueError(
                "Slack token is missing the following required scopes: {}".format(
                    sorted(missing_scopes)
                )
            )
    else:
        raise Exception("Unknown token type -- must start with xoxb- or xoxp-")