Archie.bpgc Archie.bpgc - 2 months ago 48
Android Question

PERMISSION_DENIED while using Google Natural Language API

I set up the Service Account and downloaded the credentials json file.

GoogleNLSample-479e92642552.json


Code:

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();


Stacktrace:

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: }


enter image description here

{
"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": "***********"
}

Answer

I would recommend to test the Quickstart steps of Natural Language API here. More specifically you should first make sure that:

  • billing is enabled
  • natural language API is enabled

After you have done some testing with curl, you can start experimenting in Java.

However, note that there is a difference between API keys and the service account credentials. See here for an in depth explanation for the differences. Basically, the API key is not in the JSON file and it just provides another/easier way to interact with the API.

Edit: As noted by @archie-bpgc there was also a line missing in the initialize: callback credential.initialize(request);

Comments