If your application is web based with a url, then you can redirect them there. If it’s an actual computer application, then there isn’t a way to redirect them back. There are others on here that have experience with Ajax, they might better suited to help you with your redirect question.
As far as generating the token, You would just make a rest request to the url listed in the link I posted above while filling out the required parameters. From there, you can extract the token from the response. How do to that programmatically, I’m unsure, as I have never really done anything web based. I’ve only ever manually copied the token after generating it.