REST authentication

I want to accomplish following:

  1. read a list of words from a confluence page
  2. in a different space, for each word in the list, search for pages whose titles match the word
  3. for each page that is found, accumulate their contents and create a single page from the contents.

My first questions is:
So in order to accomplish this which should i use? Macros? user macros? Apps? or java application that calls Confluence server REST API?

My second question is:
I don’t know where to start, so I’m starting with building a java app that will call the Confluence server REST API to do the above listed things, and I am able to call the REST API using username and password using Basic Auth as described here. is it not possible to authenticate using tokens for Confluence server? I don’t want to expose my personal password into the source code for other developers to see.