Sheets API from Google App Engine

903 views Asked by At

I am trying to access my own sheets from Google App Engine. I followed the following tutorial

https://developers.google.com/sheets/quickstart/java

this code works fine as long as the code does not run inside the Google App Engine. The following line of code throws an exception (your are not allowed to use sockets)

Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");

So as you can I see, I have problems with the authorization between my app engine and my google sheets.

Has someone a working example how I can access the Google Sheets API from App Engine or can give me an example how the OAuth2 Autorization between App Engine and any other Google Service will work?

Regards

Michael

1

There are 1 answers

0
ReyAnthonyRenacia On

I think Google App Engine has guides with regard to connecting to your App like

To issue an outbound HTTP request, use java.net.URLConnection. App Engine implements the methods defined in this abstract class by using the URL Fetch API.

The following snippet demonstrates how to perform a basic HTTP GET request. The application creates a new URL object, then calls the object's openStream() method to retrieve the content at that URL:

URL url = new URL("http://api.icndb.com/jokes/random");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuffer json = new StringBuffer();
String line;

while ((line = reader.readLine()) != null) {
  json.append(line);
}
reader.close();

Since you received an error with regard to sockets, App Engine has a guide for that too:

Overview of Sockets API for Java

-Sockets are available only for paid apps.
-You cannot create a listen socket; you can only create outbound sockets.
-FTP is not supported.
-java.net.URL is still configured to use the URL Fetch API; there is currently no way around this.
-InetAddress.isReachable is a no-op, etc
  • Overview of App Identity API for Java The App Identity API lets an application discover its application ID (also called the project ID). Using the ID, an App Engine application can assert its identity to other App Engine Apps, Google APIs, and third-party applications and services. The application ID can also be used to generate a URL or email address, or to make a run-time decision.