【发布时间】:2016-09-23 16:33:54
【问题描述】:
我 set up 服务帐户并下载了凭据 json 文件。
GoogleNLSample-479e92642552.json
代码:
InputStream is = getAssets().open("GoogleNLSample-479e92642552.json");
GoogleCredential credential = GoogleCredential.fromStream(is).createScoped(CloudNaturalLanguageAPIScopes.all());
languageApi = new CloudNaturalLanguageAPI.Builder(new NetHttpTransport(),
jacksonFactory, new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
})
.setApplicationName(APPLICATION_NAME).build();
AnalyzeEntitiesRequest request =
new AnalyzeEntitiesRequest()
.setDocument(new Document().setContent(text).setType("PLAIN_TEXT").setLanguage("en"))
.setEncodingType("UTF8");
final CloudNaturalLanguageAPI.Documents.AnalyzeEntities analyze = languageApi.documents().analyzeEntities(request);
//Exception here
AnalyzeEntitiesResponse response = analyze.execute();
堆栈跟踪:
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: {
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "code" : 403,
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "errors" : [ {
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "domain" : "global",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "message" : "The request cannot be identified with a client project. Please pass a valid API key with the request.",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "reason" : "forbidden"
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: } ],
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "message" : "The request cannot be identified with a client project. Please pass a valid API key with the request.",
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: "status" : "PERMISSION_DENIED"
09-23 21:43:27.897 32562-677/com.sample.android W/System.err: }
{
"type": "service_account",
"project_id": "***********",
"private_key_id": "***********",
"private_key": "-----BEGIN PRIVATE KEY-----\n***********\n-----END PRIVATE KEY-----\n",
"client_email": "***********",
"client_id": "***********",
"auth_uri": "***********",
"token_uri": "***********",
"auth_provider_x509_cert_url": "***********",
"client_x509_cert_url": "***********"
}
【问题讨论】:
-
确认一下,您已经在 Google Developer Console 中注册了您的 APP 吗?此错误与您的 API 密钥有关,因此或者您没有发送您的密钥或您的密钥用于另一个 API
-
@LucasQueirozRibeiro 我注册了一个 API 密钥,但是在哪里使用呢?检查有问题的屏幕截图。谢谢。
-
我从未专门使用过此 API,但我认为您是从 Json (
GoogleCredential.fromStream(is)) 获取您的 api。在其他 API 中,我将清单中的密钥声明为meta-data客户端自动选择这样:mGoogleApiClient = new GoogleApiClient.Builder(getContext()) .addApi(Places.GEO_DATA_API) .build(); -
您使用的是 Json 样本对吗?可能在这个 JSON 中存在一个像
api_key: "YOUR_API_KEY"这样的条目,大多数样本都是这样来的 -
@LucasQueirozRibeiro 是的,我使用的是 JSON 样本,但里面的数据很好,因为它是从云服务下载的。
标签: android google-cloud-platform