Skip to content

Instantly share code, notes, and snippets.

@sleepless-se
Last active November 11, 2021 11:41
Show Gist options
  • Save sleepless-se/171b1607e60a180e85a6f986636eefef to your computer and use it in GitHub Desktop.
Save sleepless-se/171b1607e60a180e85a6f986636eefef to your computer and use it in GitHub Desktop.
Google Authentification file read from .jar file.
public class Utility {
public static void main(String[] args) throws Exception {
Utility utility = new Utility();
GoogleCredentials credentials = utility.getGoogleCredentials("./credentials.json");
}
GoogleCredentials getGoogleCredentials(String jsonPath) throws IOException {
// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS
// environment variable.
URL url = this.getClass().getResource(jsonPath);
String path = url.getPath();
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(path))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
return credentials;
}
}
@sleepless-se
Copy link
Author

Googleの認証ファイル(credentials.json)はUtility.javaから見た相対パスで取得する。
"./credentials.json"の場合はUtility.javaと同じ階層にcredentials.jsonを置く。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment