【问题标题】:Getting a Google Speech API access token for a production Android app获取生产 Android 应用程序的 Google Speech API 访问令牌
【发布时间】:2017-03-30 10:09:03
【问题描述】:

我在 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 提供的Application Default Credentials,但我不知道如何使用访问令牌来实际响应我的应用。

【问题讨论】:

    标签: android google-speech-api google-cloud-speech


    【解决方案1】:

    我是示例的作者。

    这是应该在服务器端的部分。基本上,您必须将 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 找到您使用的语言的客户端库。

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

    【讨论】:

    • 非常感谢,Yuichi,这很有意义!我没想到会这么容易:)。
    【解决方案2】:

    对于 Android,您可能希望使用 Android sample for Google Cloud Speech,因为云客户端库目前不专注于 Android 支持。

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 1970-01-01
      • 2017-04-11
      • 2013-06-09
      • 2012-03-29
      • 2016-01-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      相关资源
      最近更新 更多