You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Business Messaging OAuth权限范围无效及接口权限不足问题

关于Google Business Messages标记已读接口的权限范围问题

需求背景

需要通过OAuth获取已开通Business Messaging的客户企业的Google My Business API权限,实现两个核心功能:回复商务消息、用户发送消息时将其标记为已读。根据官方文档,标记消息已读需使用权限范围 https://www.googleapis.com/auth/businessmessages。

问题现象

  1. 权限范围添加失败:将https://www.googleapis.com/auth/businessmessages添加到OAuth同意设置时,系统提示该范围无效。
  2. 接口调用权限不足:当前OAuth访问令牌包含的权限范围如下:
https://www.googleapis.com/auth/businesscommunications
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/business.manage

调用标记已读接口时返回403权限错误:

{
    "error": {
        "code": 403,
        "message": "Request had insufficient authentication scopes.",
        "status": "PERMISSION_DENIED",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.ErrorInfo",
                "reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT",
                "domain": "googleapis.com",
                "metadata": {
                    "method": "google.communications.businessmessages.v1.BusinessMessagesService.UpdateReceipt",
                    "service": "businessmessages.googleapis.com"
                }
            }
        ]
    }
}

相关截图与代码

OAuth同意界面截图

OAuth同意界面截图

生成OAuth同意界面URL的代码

const { google } = require("googleapis");

const oauth2Client = new google.auth.OAuth2(
  "clientId",
  "clientSecret",
  "http://127.0.0.1"
);

const scopes = [
  "https://www.googleapis.com/auth/business.manage",
  "https://www.googleapis.com/auth/businesscommunications",
  "https://www.googleapis.com/auth/businessmessages",
  "https://www.googleapis.com/auth/userinfo.email",
  "https://www.googleapis.com/auth/userinfo.profile",
];

const authorizationUrl = oauth2Client.generateAuthUrl({
  access_type: "offline",
  scope: scopes,
  include_granted_scopes: true,
});

注:由于生成的URL会返回无效范围错误,实际scopes数组中并未包含https://www.googleapis.com/auth/businessmessages

标记消息已读的代码

async function initAuth() {
    const oauth2Client = new google.auth.OAuth2(
        "clientId", // 我方GCP配置信息,非客户方
        "clientSecret", // 我方GCP配置信息,非客户方
        "http://127.0.0.1" 
        );

    oauth2Client.setCredentials({
        access_token: 'token', // 从授权页面获取的令牌
        scope: 'https://www.googleapis.com/auth/businessmessages',
    });

    return oauth2Client;
}

async function sendReadReceipt(conversationId, messageId) {
    const auth = await initAuth();

  let apiParams = {
    auth: auth,
    name: 'conversations/' + conversationId + '/messages/' + messageId + '/receipt',
    resource: {
      receiptType:'READ'
    }
  };

  bmApi.conversations.messages.updateReceipt(apiParams,
    {auth: auth}, (err, response) => {
    console.log(err);
    console.log(response);
  });
}

内容的提问来源于stack exchange,提问作者DanailYordanov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 02:45:01