【发布时间】:2022-06-23 11:42:43
【问题描述】:
目前我可以使用 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"
}
}
我知道这应该像我们调用 Google 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 <----
}
});
任何帮助将不胜感激。
【问题讨论】:
标签: node.js typescript google-api google-oauth