def _create_token()

in azure-devops/azext_devops/dev/common/github_credential_manager.py [0:0]


    def _create_token(self, note=None):
        logger.warning('We need to create a Personal Access Token to communicate with GitHub. '
                       'A new PAT with scopes (admin:repo_hook, repo, user) will be created.')
        logger.warning('You can set the PAT in the environment variable (%s) to avoid getting prompted.',
                       AZ_DEVOPS_GITHUB_PAT_ENVKEY)
        self.username = prompt(msg='Enter your GitHub username (leave blank for using already generated PAT): ')
        print('')
        if not self.username:
            while not self.token:
                self.token = prompt_pass(msg='Enter your GitHub PAT: ', help_string='Generate a Personal Access Token '
                                         'with approproate permissions from GitHub Developer settings and paste here.')
            print('')
            return
        self.password = prompt_pass(msg='Enter your GitHub password: ', confirm=True)
        print('')
        if not note:
            note = "AzureDevopsCLIExtensionToken_" + datetime_now_as_string()
        encoded_pass = base64.b64encode(self.username.encode('utf-8') + b':' + self.password.encode('utf-8'))
        basic_auth = 'basic ' + encoded_pass.decode("utf-8")
        request_body = {
            'scopes': [
                'admin:repo_hook',
                'repo',
                'user'
            ],
            'note': note
        }
        headers = {'Content-Type': 'application/json' + '; charset=utf-8',
                   'Accept': 'application/json',
                   'Authorization': basic_auth}
        response = self.post_authorization_request(headers=headers, body=request_body)
        if (response.status_code == 401 and response.headers.get('X-GitHub-OTP') and
                response.headers.get('X-GitHub-OTP').startswith('required')):
            two_factor_code = None
            while not two_factor_code:
                two_factor_code = prompt_pass(msg='Enter your two factor authentication code: ')
            print('')
            headers = {'Content-Type': 'application/json' + '; charset=utf-8',
                       'Accept': 'application/json',
                       'Authorization': basic_auth,
                       'X-GitHub-OTP': two_factor_code}
            response = self.post_authorization_request(headers=headers, body=request_body)
        import json
        response_json = json.loads(response.content)
        if response.status_code == 200 or response.status_code == 201:
            logger.warning('Created new personal access token with scopes (admin:repo_hook, repo, user). Name: %s '
                           'You can revoke this from your GitHub settings if the pipeline is no longer required.',
                           note)
            self.token = response_json['token']
        else:
            raise CLIError('Could not create a Personal Access Token for GitHub. Check your credentials and try again.')