使用API​​密钥在Android上通过GRPC验证Google Cloud Speech

使用API​​密钥在Android上通过GRPC验证Google Cloud Speech,第1张

概述我已经设法通过GRPC使用流媒体模式的服务帐户为我的 Android应用程序运行Google Cloud Speech.但是,根据我所读到的内容,出于安全原因,我不应该在其中部署具有这些凭据的Android应用程序(当前存储为资源中的JSON文件).正确的是创建一个API密钥,如下所述: https://cloud.google.com/speech/docs/common/auth 这允许我限制 我已经设法通过GRPC使用流媒体模式的服务帐户为我的 Android应用程序运行Google Cloud Speech.但是,根据我所读到的内容,出于安全原因,我不应该在其中部署具有这些凭据的AndroID应用程序(当前存储为资源中的JsON文件).正确的是创建一个API密钥,如下所述: https://cloud.google.com/speech/docs/common/auth

这允许我限制对我的特定AndroID应用的访问.但是,我一直无法找到如何使用GRPC的API密钥.我目前正在从JsON文件创建一个GoogleCredentials实例,这很好用.如何从API密钥获取凭证对象?

解决方法 您可以使用API​​密钥尝试此 *** 作

final ManagedChannel channel = new OkhttpChannelProvIDer()    .builderForAddress(HOSTname,PORT)    .nameResolverFactory(new DnsnameResolverProvIDer())    .intercept(new Interceptor(API_KEY))    .build();speechStub = SpeechGrpc.newStub(channel);private static final class Interceptor implements ClIEntInterceptor {  private final String APIKey;  private static Metadata.Key<String> API_KEY_header =      Metadata.Key.of("x-goog-API-key",Metadata.ASCII_STRING_MARSHALLER);  public Interceptor(String APIKey) {    this.APIKey = APIKey;  }  @OverrIDe  public <ReqT,RespT> ClIEntCall<ReqT,RespT> interceptCall(      MethodDescriptor<ReqT,RespT> method,CallOptions callOptions,Channel next) {    ClIEntCall<ReqT,RespT> call = next.newCall(method,callOptions);    call = new ForwardingClIEntCall.SimpleForwardingClIEntCall<ReqT,RespT>(call) {      @OverrIDe      public voID start(Listener<RespT> responseListener,Metadata headers) {        if (APIKey != null && !APIKey.isEmpty()) {          headers.put(API_KEY_header,APIKey);        }        super.start(responseListener,headers);      }    };    return call;  }}
总结

以上是内存溢出为你收集整理的使用API​​密钥在Android上通过GRPC验证Google Cloud Speech全部内容,希望文章能够帮你解决使用API​​密钥在Android上通过GRPC验证Google Cloud Speech所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1209850.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-04
下一篇2022-06-04

发表评论

登录后才能评论

评论列表(0条)

    保存