2

我在 Android 应用程序中使用 Google Speech API。自述文件指出: In this sample, we load the credential from a JSON file stored in a raw resource folder of this client app. You should never do this in your app. Instead, store the file in your server and obtain an access token from there.

是否有关于如何正确获取生产应用程序的访问令牌的示例?

从我收集的信息来看,我似乎可以使用通过 Compute Engine 或 GAE 提供的应用程序默认凭据,但我不知道如何使用访问令牌实际响应我的应用程序。

4

2 回答 2

4

我是样本的作者。

这是应该在服务器端的部分。基本上,您必须将 JSON 文件安全地存储在服务器端,并在收到来自移动应用程序的请求时获取新的访问令牌。移动应用程序应始终与您的服务器通信以获取访问令牌。这样,您无需将服务帐户密钥放入 APK 中。

        // In response to a request from a mobile client

        // Open the JSON file stored on the server side.
        final InputStream stream = ...;
        try {
            // Initialize the credentials
            final GoogleCredentials credentials =
                GoogleCredentials.fromStream(stream)
                    .createScoped(SCOPE);
            // Fetch a new access token.
            final AccessToken token = credentials.refreshAccessToken();

            // Return the token to the mobile app.

        } catch (IOException e) {

            // Maybe report this as an HTTP error.

        }

如果您不在服务器端使用 Java,您可以在Google Cloud Client Libraries找到您使用的语言的客户端库。

有关如何将请求从后端发送到 API 的基本信息,请参阅Google Cloud Platform Auth Guide 。

于 2017-04-04T09:15:53.160 回答
1

对于 Android,您可能希望使用Google Cloud Speech 的 Android 示例,因为 Cloud 客户端库当前不关注 Android 支持。

于 2017-04-04T01:04:46.460 回答