我试图通过调用动作API“ https://actions.googleapis.com/v2/conversations:send”来向Google Assistant发送推送通知。我越来越 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,用于向用户发送推送通知。
但收到错误:
{ “错误”:{ “代码”:403, “ message”:“呼叫者没有权限”, “状态”:“ PERMISSION_DENIED” } }
答案 0 :(得分:0)
有关如何通过Actions API使用Java客户端库正确发送推送通知的信息,请参见用Java编写的AoG Updates and Push Notifications code sample。