我一直在尝试实现Google的自然语言API。当我去验证我的凭证时,它的净值有问题。关于如何解决这个问题的任何线索?我正在使用Java8 btw。我在其他一些线程上看到这是一个Java版本。
String text = "My name is Teneala. I ran last week.";
InputStream is = getResources().openRawResource(R.raw.credentials);
try {
GoogleCredential credential = null;
credential = GoogleCredential.fromStream(is);
Collection<String> scopes = Collections.singleton("https://www.googleapis.com/auth/cloud-language");
if (credential.createScopedRequired()) {
credential = credential.createScoped(scopes);
}
// copy over key values, note the additional "s", set some expiry
// com.google.auth.oauth2.GoogleCredentials
GoogleCredentials sac = ServiceAccountCredentials.newBuilder()
.setPrivateKey(credential.getServiceAccountPrivateKey())
.setPrivateKeyId(credential.getServiceAccountPrivateKeyId())
.setClientEmail(credential.getServiceAccountId())
.setScopes(scopes)
.setAccessToken(new AccessToken(credential.getAccessToken(), null))
.build();
// Latest generation Google libs, GoogleCredentials extends Credentials
CredentialsProvider cp = FixedCredentialsProvider.create(sac);
LanguageServiceSettings settings = (LanguageServiceSettings) LanguageServiceSettings.newBuilder().setCredentialsProvider(cp).build();
LanguageServiceClient language = LanguageServiceClient.create(settings);
Document doc = Document.newBuilder()
.setContent(text)
.setType(Type.PLAIN_TEXT)
.build();
AnalyzeSyntaxRequest request = AnalyzeSyntaxRequest.newBuilder()
.setDocument(doc)
.setEncodingType(EncodingType.UTF16)
.build();
// analyze the syntax in the given text
AnalyzeSyntaxResponse response = language.analyzeSyntax(request);
// print the response
for (Token token : response.getTokensList()) {
System.out.printf("\tText: %s\n", token.getText().getContent());
System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset());
System.out.printf("Lemma: %s\n", token.getLemma());
System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag());
System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect());
System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase());
System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm());
System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender());
System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood());
System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber());
System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson());
System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper());
System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity());
System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense());
System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice());
System.out.println("DependencyEdge");
System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex());
System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
```
这是我的gradle文件。我尝试排除它,研究了疑难解答文档,尝试实现它,等等。
dependencies {
// Google APIs
implementation('com.google.cloud:google-cloud-translate:1.12.0') {
exclude group: 'org.apache.httpcomponents'
exclude group: 'org.google', module: 'google'
exclude group: 'com.google.auto.value', module: 'auto-value'
//exclude 'io.netty:netty-tcnative:2.0.20.Final:'
}
annotationProcessor 'com.google.cloud:google-cloud-translate:1.12.0'
implementation ('com.google.cloud:google-cloud-language:1.14.0'){
exclude group: 'com.google.auto.value', module: 'auto-value'
//exclude 'io.netty:netty-tcnative:2.0.20.Final:'
}
}
答案 0 :(得分:0)
没关系。我在这里找到了解决方案:https://github.com/grpc/grpc-java/blob/master/SECURITY.md#troubleshooting。我必须将此添加到应用程序构建gradle。 Android设备不支持netty。
implementation 'io.grpc:grpc-okhttp:1.7.0'
implementation 'com.squareup.okhttp3:okhttp:3.0.1'