哪个是Hello World最简单的Google API?

时间:2019-06-09 04:47:49

标签: java google-api cloud distributed-computing google-api-java-client

要调用的“ 最简单” Google API调用是什么? Google不会似乎对该密钥有问题,他们只是想要设置结算选项。严格要求吗?我不打算锤打他们的服务器。

最终,希望运行一些映射查询。首先,什么是Google服务或API would work as a preliminary step towards using映射API?

Google翻译似乎是一项付费服务​​:

thufir@dur:~/NetBeansProjects/helloWorldJavaFX$ 
thufir@dur:~/NetBeansProjects/helloWorldJavaFX$ gradle clean run

> Task :run FAILED
Jun 08, 2019 9:35:43 PM net.bounceme.dur.fx.SpikeGoogleTranslate translate
INFO: key               AIzaSyDtE0WRh9l5AIlH6LSKYeeEKioquSWr1UQ
Jun 08, 2019 9:35:43 PM net.bounceme.dur.fx.SpikeGoogleTranslate translate
INFO: from              90210
Jun 08, 2019 9:35:43 PM net.bounceme.dur.fx.SpikeGoogleTranslate translate
INFO: to                90210
Jun 08, 2019 9:35:43 PM net.bounceme.dur.fx.SpikeGoogleTranslate translate
INFO: text              java
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "usageLimits",
    "message" : "Cloud Translation API has not been used in project 198801459119 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/translate.googleapis.com/overview?project=198801459119 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
    "reason" : "rateLimitExceeded"
  } ],
  "message" : "Cloud Translation API has not been used in project 198801459119 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/translate.googleapis.com/overview?project=198801459119 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
  "status" : "PERMISSION_DENIED"
}
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:150)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
        at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:417)
        at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1089)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:515)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:448)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)
        at net.bounceme.dur.fx.SpikeGoogleTranslate.translate(SpikeGoogleTranslate.java:45)
        at net.bounceme.dur.fx.App.runApp(App.java:20)
        at net.bounceme.dur.fx.App.main(App.java:14)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
4 actionable tasks: 4 executed
thufir@dur:~/NetBeansProjects/helloWorldJavaFX$ 

代码:

package net.bounceme.dur.fx;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.translate.Translate;
import com.google.api.services.translate.TranslateRequestInitializer;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.logging.Logger;

public class SpikeGoogleTranslate {

    private static final Logger log = Logger.getLogger(SpikeGoogleTranslate.class.getName());

    public void translate(Properties properties) throws IOException, GeneralSecurityException {
        String key = properties.getProperty("api_key");
        String originalLanguage = properties.getProperty("original_language");
        String translatedLanguage = properties.getProperty("translated_language");
        String text = properties.getProperty("text_to_translate");

        log.info("key\t\t" + key);
        log.info("from\t\t" + originalLanguage);
        log.info("to\t\t" + translatedLanguage);
        log.info("text\t\t" + text);

        // set key created via google cloud console
        final TranslateRequestInitializer KEY_INITIALIZER = new TranslateRequestInitializer(key);

        // Set up the HTTP transport and JSON factory
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        // set up translate
        final Translate translate = new Translate.Builder(httpTransport, jsonFactory, null)
                .setApplicationName("My Project")
                .setTranslateRequestInitializer(KEY_INITIALIZER)
                .build();

        // list languages
        {
            System.out.println(translate.languages().list().execute());
            // output: {"languages":[{"language":"af"},{"language":"ar"},{"language":"az"},{"language":"be"},{"language":"bg"},{"language":"bn"},{"language":"bs"},{"language":"ca"},{"language":"ceb"},{"language":"cs"},{"language":"cy"},{"language":"da"},{"language":"de"},{"language":"el"},{"language":"en"},{"language":"eo"},{"language":"es"},{"language":"et"},{"language":"eu"},{"language":"fa"},{"language":"fi"},{"language":"fr"},{"language":"ga"},{"language":"gl"},{"language":"gu"},{"language":"ha"},{"language":"hi"},{"language":"hmn"},{"language":"hr"},{"language":"ht"},{"language":"hu"},{"language":"hy"},{"language":"id"},{"language":"ig"},{"language":"is"},{"language":"it"},{"language":"iw"},{"language":"ja"},{"language":"jw"},{"language":"ka"},{"language":"kk"},{"language":"km"},{"language":"kn"},{"language":"ko"},{"language":"la"},{"language":"lo"},{"language":"lt"},{"language":"lv"},{"language":"mg"},{"language":"mi"},{"language":"mk"},{"language":"ml"},{"language":"mn"},{"language":"mr"},{"language":"ms"},{"language":"mt"},{"language":"my"},{"language":"ne"},{"language":"nl"},{"language":"no"},{"language":"ny"},{"language":"pa"},{"language":"pl"},{"language":"pt"},{"language":"ro"},{"language":"ru"},{"language":"si"},{"language":"sk"},{"language":"sl"},{"language":"so"},{"language":"sq"},{"language":"sr"},{"language":"st"},{"language":"su"},{"language":"sv"},{"language":"sw"},{"language":"ta"},{"language":"te"},{"language":"tg"},{"language":"th"},{"language":"tl"},{"language":"tr"},{"language":"uk"},{"language":"ur"},{"language":"uz"},{"language":"vi"},{"language":"yi"},{"language":"yo"},{"language":"zh"},{"language":"zh-TW"},{"language":"zu"}]}
        }

        // translate
        {
            // phrases
            final ImmutableList<String> phrasesToTranslate = ImmutableList.<String>builder().add("Hello world").add("Where can I walk my dog").build();
            // perform
            System.out.println(translate.translations().list(phrasesToTranslate, "fr").execute());
            // output: {"translations":[{"detectedSourceLanguage":"en","translatedText":"Bonjour le monde"},{"detectedSourceLanguage":"en","translatedText":"Où puis-je promener mon chien"}]}
        }
    }

}

代码来自gist

1 个答案:

答案 0 :(得分:1)

据我所知,我们过去经常由于两个原因而导致此错误。

要么在项目中创建了一个新的APP,要么我们未启用它。或者,我们超过了每日/总点击次数限制。

您可以在此处获得全面的见识,

https://developers.google.com/drive/api/v3/handle-errors#403_daily_limit_exceeded

https://cloud.google.com/apis/docs/enable-disable-apis