This is somewhat of a general Java question, but I do believe it has an
IDE-specific component.

What are the best practices for storing and retrieving API keys in Java
programs? And what help does NetBeans offer for adhering to those best
practices?

Suppose for example that your key for an example widget API is
"555EXAMPLE." You could certainly write "private final static String
API_KEY = "555EXAMPLE";" and then each time you need the key, you write "
API_KEY" where it's needed.

String query = "https://www.example.com/api/q="; + sendParams + "&key=" +
API_KEY;
URL queryURL = new URL(query);
HttpURLConnection conn = (HttpURLConnection) queryURL.openConnection();
conn.setRequestMethod("POST");
// etc.

But then I might forget about the API key and upload the source file to a
public GitHub repository (maybe GitHub would alert us, but I don't know for
sure).

I suppose I could store the API key in a file or folder listed in the Git
Ignore, and then create a class to store and retrieve API keys, but that
would probably feel like I'm reinventing the wheel...

Alonso del Arte
Author at SmashWords.com
<https://www.smashwords.com/profile/view/AlonsoDelarte>
Musician at ReverbNation.com <http://www.reverbnation.com/alonsodelarte>

Reply via email to