Last active
November 11, 2021 11:41
-
-
Save sleepless-se/171b1607e60a180e85a6f986636eefef to your computer and use it in GitHub Desktop.
Google Authentification file read from .jar file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Googleの認証ファイル(credentials.json)はUtility.javaから見た相対パスで取得する。
"./credentials.json"の場合はUtility.javaと同じ階層にcredentials.jsonを置く。