Similar questions to this one were posted, but none of the answers seems to help in my case. I am writing a package that uses Google's credentials to obtain Google Apps users. For this, I am using a service account, and so in order to retrieve the credentials, I need to provide (among others) a p12 signature file:
Credential credential = null;
try {
credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccountEmail)
.setServiceAccountScopes(SCOPES)
.setServiceAccountUser(serviceAccountUser)
.setServiceAccountPrivateKeyFromP12File( java.io.File ))
The last function must receive a java.io.File object of the p12 signature file. Now, this whole thing runs inside a jar I am providing to others, and that creates most of the problems. I couldn't read the file when running inside the jar no matter which approach I took. Among things I tried:
return new File(GoogleUserFactory.class.getClassLoader().getResource("/" + filePath).toURI());
// (I also tried without the "/" and using class.GetResource() directly)
URL url = GoogleUserFactory.class.getResource("/" + filePath);
return new File(url.getPath());
// I also tried class.GetClassLoader.GetResource()...
Even tried to read the file as a InputStreamReader and write to a new File (which after I will create a File object for and return it), but since I am in a jar, I cannot seem to have permissions to write a new file)
可能是你可以使用.setServiceAccountPrivateKey(SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), SOME_INPUT_STREAM, "notasecret", "privatekey", "notasecret");
作为建议在这里 ?
Please try without getClassLoader(). Probably you tried it. but it must work. if you receive NullPointerException filePath can be wrong.
File file = new File(GoogleUserFactory.class.getResource("/" + filePath).toURI());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.