[英]Getting 403- PERMISSION_DENIED on calling actions api
I was trying to send push notification to google assistant by calling actions api " https://actions.googleapis.com/v2/conversations:send ".我试图通过调用操作 api“ https://actions.googleapis.com/v2/conversations:send ”来向谷歌助手发送推送通知。 I'm getting 403-The caller does not have permission error.我收到 403 - 调用者没有权限错误。
I followed the steps mentioned in the push notification documentation.我按照推送通知文档中提到的步骤进行操作。 " https://developers.google.com/actions/assistant/updates/notifications#java ". “ 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.向用户发送推送通知的预期操作 api。
But getting the error:但得到错误:
{ "error": { "code": 403, "message": "The caller does not have permission", "status": "PERMISSION_DENIED" } } { "error": { "code": 403, "message": "调用方没有权限", "status": "PERMISSION_DENIED" } }
请参阅用 Java 编写的AoG 更新和推送通知代码示例,了解如何通过 Actions API 使用 Java 客户端库正确发送推送通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.