How can I use java.io.File(filePath) in AppEngine?

2,040 views
Skip to first unread message

Juan de Dios Becerra

unread,
Jan 21, 2014, 4:55:53 PM1/21/14
to google-a...@googlegroups.com
I am trying to use Google + Domains API in GAE, but when I try to create a GoogleCredential I need to use this:

.setServiceAccountPrivateKeyFromP12File(new java.io.File(filePath))

Where filePath is a .p12 extension file(private key for authentication) this file I put in WEB-INF folder and I granted permissions to all, but when I try to create the GoogleCredential I get this error:

java.security.AccessControlException: access denied (java.io.FilePermission /WEB-INF/file.p12 read)

Obviously the file has the correct name in my folder, this process works in my project which is not AppEngine, so I guess is a problem in the way to access files in AppEngine using java.io.File.

If somebody has idea, I would be very thankful.

Vinny P

unread,
Jan 21, 2014, 11:39:14 PM1/21/14
to google-a...@googlegroups.com
Java I/O classes, such as File, are limited on App Engine. Try using ServletContext's stream services to collect an inputstream, then read the file normally. For example:

InputStream stream = this.getServletContext().getResourceAsStream(path);

 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com

Juan de Dios Becerra

unread,
Jan 22, 2014, 9:52:16 AM1/22/14
to google-a...@googlegroups.com
What you mean when say "then read file normally" is that I can use the same new ava.io.File(filePath) function?

I guess this line:

InputStream stream = this.getServletContext().getResourceAsStream(path);

is only to collect the inputsream that you refer, but I don't use stream variable?

Alejandro Gonzalez

unread,
Jan 22, 2014, 10:33:43 AM1/22/14
to google-a...@googlegroups.com
Hello Juan, maybe with an example is more clear:

            URI keyURL;
            String fileName = "google_bigq.p12";
            keyURL = BigQueryAPIManager.class.getClassLoader().getResource(fileName).toURI();

            GoogleCredential credential = new GoogleCredential.Builder().setTransport(TRANSPORT)
                    .setJsonFactory(JSON_FACTORY)
                    .setServiceAccountId(EMAIL_ID)
                    .setServiceAccountScopes(SCOPES)
                    .setServiceAccountPrivateKeyFromP12File(new File(keyURL))
                    .build();

Regards,
Alejandro



2014/1/22 Juan de Dios Becerra <j.bece...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/groups/opt_out.

Nick

unread,
Jan 22, 2014, 5:50:59 PM1/22/14
to google-a...@googlegroups.com
This is how we load it during our DI phase:

String certificate = "/someuniqueid-privatekey.p12";

File privateKey = new File(getClass().getResource(certificate).toURI());

...

builder.setServiceAccountPrivateKeyFromP12File(privateKey);


In this example, the p12 file is in our src/main/resources.

Vizay Soni

unread,
Apr 7, 2014, 3:17:20 AM4/7/14
to google-a...@googlegroups.com
You could also use following:

inputStream = getClass().getClassLoader().getResourceAsStream(APNS_CERTIFICATE_PATH);
Reply all
Reply to author
Forward
0 new messages