在调用动作api时获得403-PERMISSION_DENIED

问题描述 投票:-1回答:1

我试图通过调用动作api“https://actions.googleapis.com/v2/conversations:send”向谷歌助理发送推送通知。我得到403-调用者没有权限错误。

我按照推送通知文档中提到的步骤操作。 “https://developers.google.com/actions/assistant/updates/notifications#java”。

创建服务帐户密钥并将角色指定为项目所有者。

    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"));
}    

期望的动作api向用户发送推送通知。

但得到错误:

{“error”:{“code”:403,“message”:“来电者没有权限”,“状态”:“PERMISSION_DENIED”}}

java push-notification google-oauth actions-on-google google-assist-api
1个回答
0
投票

请参阅使用Java编写的AoG Updates and Push Notifications code sample,了解如何通过Actions API使用Java客户端库正确发送推送通知。

© www.soinside.com 2019 - 2024. All rights reserved.