How to create API token automatically?

See this answer: How to get the API key from REST API or other scripts?