Retrieve Google Drive file metadata without using OAuth2.0

Using Eclipse, I’m testing a method to obtain public metadata from Google Drive files via an API key. Example:

public void displayInfo() {
    DriveConnector connector = new DriveConnector("API_KEY");
    MetaData details = connector.fetchDetails("FILE_ID");
    System.out.println(details.getIdentifier());
}

Is it secure to embed my API key directly in the code?

Embedding the API key directly in your code raises security concerns. Even though you are only accessing public metadata, the API key can still be extracted and abused, potentially leading to unauthorized actions or quota theft. In my experience, it is better to externalize sensitive information like API keys by storing them in environment variables or secure configuration files. Doing so minimizes the risk of accidental exposure, especially when managing your code in shared or public repositories. It’s a simple practice that enhances overall security.

i think its better to not hard code the apikey. even if its for public data there is still risk of theft. using a env var or config file is a safer route as it makes it harder for nosy people to grab your key.

From my experience, embedding the API key directly in your code is generally not advised, even for public data. While risks like unauthorized access primarily affect write operations or account-specific actions, exposing any key could allow someone to use your quota or probe for vulnerabilities. A secure practice I followed was to store API keys externally and load them at runtime. This not only minimizes risk of key leakage in version control but also makes key rotation less disruptive to the deployment process.