-1

I was trying to send push notification to google assistant by calling actions api "https://actions.googleapis.com/v2/conversations:send". I'm getting 403-The caller does not have permission error.

I followed the steps mentioned in the push notification documentation. "https://developers.google.com/actions/assistant/updates/notifications#java".

Created service account key and assigned the Role as Project Owner.

    String token = getAccessToken();
    HttpPost request = new HttpPost("https://actions.googleapis.com/v2/conversations:send");
    request.setHeader("Content-type", "application/json");
    request.setHeader("Authorization", "Bearer " + token);
    StringEntity entity = new StringEntity("{\n" +
            "  \"customPushMessage\": {\n" +
            "    \"target\": {\n" +
            "      \"userId\": \"id of user\",\n" +
            "      \"intent\": \"intent name\",\n" +
            "      \"locale\": \"en-US\"\n" +
            "    },\n" +
            "    \"userNotification\": {\n" +
            "      \"title\": \"title\",\n" +
            "      \"text\": \"test msg\"\n" +
            "    }\n" +
            "  }\n" +
            "}");
    entity.setContentType(ContentType.APPLICATION_JSON.getMimeType());
    request.setEntity(entity);
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpResponse res = httpClient.execute(request);
 private String getAccessToken() throws IOException {
    AccessToken token = loadCredentials().refreshAccessToken();
    return token.getTokenValue();
}
 private ServiceAccountCredentials loadCredentials() throws IOException {
    String actionsApiServiceAccountFile =
            this.getClass().getClassLoader().getResource("/serviceaccountkey.json").getFile();
    InputStream actionsApiServiceAccount = new FileInputStream(actionsApiServiceAccountFile);
    ServiceAccountCredentials serviceAccountCredentials =
            ServiceAccountCredentials.fromStream(actionsApiServiceAccount);
    return (ServiceAccountCredentials)
            serviceAccountCredentials.createScoped(
                    Collections.singleton(
                            "https://www.googleapis.com/auth/actions.fulfillment.conversation"));
}    

Expected actions api to send push notification to the user.

But getting the error:

{ "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED" } }

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
venom
  • 1

1 Answers1

0

Refer to the AoG Updates and Push Notifications code sample written in Java for how to properly send a push notification using the Java client library via the Actions API.

Taylor Caldwell
  • 381
  • 2
  • 7