【问题标题】:Sheets API from Google App Engine来自 Google App Engine 的 Sheets API
【发布时间】:2016-12-21 20:50:19
【问题描述】:

我正在尝试从 Google App Engine 访问我自己的工作表。我按照以下教程进行操作

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

只要代码不在 Google App Engine 内运行,此代码就可以正常工作。下面这行代码抛出异常(你不能使用sockets)

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

如您所见,我的应用引擎和 Google 表格之间的授权存在问题。

是否有人提供了我如何从 App Engine 访问 Google Sheets API 的工作示例,或者可以给我一个示例,App Engine 和任何其他 Google 服务之间的 OAuth2 Autorization 将如何工作?

问候

迈克尔

【问题讨论】:

    标签: google-app-engine google-sheets-api service-accounts


    【解决方案1】:

    我认为 Google App Engine 有关于连接到您的应用的指南,例如

    要发出出站 HTTP 请求,请使用 java.net.URLConnection。 App Engine 使用 URL Fetch API 来实现此抽象类中定义的方法。

    下面的 sn-p 演示了如何执行一个基本的 HTTP GET 请求。应用程序创建一个新的 URL 对象,然后调用该对象的 openStream() 方法来检索该 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();
    

    由于您收到有关套接字的错误,因此 App Engine 也有相应的指南:

    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 App Identity API 允许应用程序发现其应用程序 ID(也称为项目 ID)。使用 ID,App Engine 应用程序可以向其他 App Engine 应用程序、Google API 以及第三方应用程序和服务声明其身份。应用程序 ID 还可用于生成 URL 或电子邮件地址,或用于做出运行时决策。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 2020-05-12
      相关资源
      最近更新 更多