使用访问令牌使用Google Business Messages发送消息



目前我可以使用Google Business Messages API从代理向NodeJS代码的用户发送消息。

const bmApi = new businessmessages.businessmessages_v1.Businessmessages({});

这需要给定服务帐户密钥/机密的身份验证客户端。

const auth = new GoogleAuth({
keyFilename: '/home/my-keyfile.json',
scopes: 'https://www.googleapis.com/auth/businessmessages',
});
const authClient = await auth.getClient();
// and logic to send message

然而,密钥/秘密目前是硬编码的。

但在流程的这一点上,我有了访问令牌。

并且希望使用它来代替.json文件。

但它不会接受访问令牌。

另一种方法是直接调用REST接口。https://developers.google.com/business-communications/business-messages/guides/how-to/message/send

curl -X POST https://businessmessages.googleapis.com/v1/conversations/__CONVERSATION_ID__/messages 
-H "Content-Type: application/json" 
-H "User-Agent: curl/business-messages" 
-H "$(oauth2l header --json ./service_account_key.json businessmessages)" 
-d "{
'messageId': '$(uuidgen)',
'text': 'Hello world!',
'representative': {
'avatarImage': 'https://developers.google.com/identity/images/g-logo.png',
'displayName': 'Chatbot',
'representativeType': 'BOT'
}
}"

添加了一个带有标记的标头。

access_token: <access-token>

但再次没有快乐。

{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}

我知道这应该像我们对谷歌Play商店的调用一样有效:

try {
let response = await this.httpClient.post({
url: `${process.env.PLAYSTORE_URL}/${packageName}/reviews/${reviewId}:reply`,
body : {
"replyText" : replyText
},
query: {
access_token: access_token <----
}
});

任何帮助都将不胜感激。

我认为您需要使用与url路径中当前CONVERSATION_ID匹配的变量,以及当前接收到的每个代理消息中的一个。

Example:
curl -X POST https://businessmessages.googleapis.com/v1/conversations/$(uuidgen)/messages 
-H "Content-Type: application/json" 
-H "User-Agent: curl/business-messages" 
-H "$(oauth2l header --json ./service_account_key.json businessmessages)" 
-d "{
'messageId': '$(uuidgen)',
'text': 'Hello world!',
'representative': {
'avatarImage': 'https://developers.google.com/identity/images/g-logo.png',
'displayName': 'Chatbot',
'representativeType': 'BOT'
}
}"

最新更新