使用Google RCS Business Messaging发送消息的参数配置及API报错问题
解决Google RCS Business Messaging PHP发送消息的参数替换与403权限问题
一、参数替换的正确方式
针对你提到的三个参数,按以下规则替换:
- REGION:印度对应的区域代码为
asia-south1,直接替换API endpoint中的REGION占位符即可。 - MESSAGE_ID:需生成符合RFC 4122标准的唯一UUID,用于追踪单条消息。PHP中可通过
uuid_create(UUID_TYPE_RANDOM)(需PHP 7.0+且启用uuid扩展)生成,或自行构造格式如xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx的字符串,确保每条消息ID唯一。 - PATH_TO_SERVICE_ACCOUNT_KEY:填写从Google Cloud控制台下载的服务账号JSON密钥文件的本地绝对路径,例如
/var/www/rcs-service-key.json(Linux)或C:\projects\rcs\service-key.json(Windows)。
二、403权限拒绝错误的排查与解决
按以下步骤逐一验证:
- 确认服务账号权限
登录Google Cloud IAM控制台,找到使用的服务账号,为其分配RCS Business Messaging Sender角色(或直接添加rcsbusinessmessaging.messages.create权限),确保账号拥有发送消息的权限。 - 验证API启用状态
检查当前Google Cloud项目中,RCS Business Messaging API已正确启用,且未选错项目(需与RCS Agent归属项目一致)。 - 核对密钥文件有效性
确认使用的JSON密钥文件属于当前项目的服务账号,文件未过期、内容未被篡改。 - 检查Agent关联关系
在Business Communications Console中,确认你的RCS Agent已关联到当前使用的Google Cloud项目,Agent的状态为已激活。 - 验证手机号格式
接收消息的手机号必须严格遵循E.164格式,印度手机号示例:+91XXXXXXXXXX,不能包含空格、括号或其他特殊字符。
三、PHP发送消息代码示例
方式1:使用curl命令+gcloud获取令牌
<?php // 替换为你的实际参数 $region = 'asia-south1'; $messageId = uuid_create(UUID_TYPE_RANDOM); $serviceAccountKeyPath = '/path/to/your/service-account.json'; $targetPhone = '+91XXXXXXXXXX'; $senderPhone = '你的Agent关联手机号(E.164格式)'; // 获取访问令牌 $tokenCmd = sprintf( 'gcloud auth application-default print-access-token --key-file=%s', escapeshellarg($serviceAccountKeyPath) ); $accessToken = trim(shell_exec($tokenCmd)); // 构造请求体 $payload = json_encode([ 'messageId' => $messageId, 'content' => ['text' => '这是一条测试RCS消息'], 'senderPhoneNumber' => $senderPhone, 'recipientPhoneNumber' => $targetPhone ]); // 发送请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://rcsbusinessmessaging.googleapis.com/v1/{$region}/phones/{$targetPhone}/messages"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', "Authorization: Bearer {$accessToken}" ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // 输出结果 if ($httpCode === 200) { echo "消息发送成功:\n" . $response; } else { echo "发送失败,状态码:{$httpCode}\n响应内容:\n" . $response; } ?>
方式2:使用Google PHP客户端库(更稳定)
首先通过Composer安装依赖:
composer require google/auth guzzlehttp/guzzle
然后编写代码:
<?php require __DIR__ . '/vendor/autoload.php'; use Google\Auth\CredentialsLoader; use GuzzleHttp\Client; // 替换为你的实际参数 $region = 'asia-south1'; $messageId = uuid_create(UUID_TYPE_RANDOM); $serviceAccountKeyPath = '/path/to/your/service-account.json'; $targetPhone = '+91XXXXXXXXXX'; $senderPhone = '你的Agent关联手机号(E.164格式)'; // 加载服务账号凭证 $credentials = CredentialsLoader::makeCredentials( ['https://www.googleapis.com/auth/rcsbusinessmessaging'], $serviceAccountKeyPath ); // 获取访问令牌 $token = $credentials->fetchAuthToken(); $accessToken = $token['access_token']; // 发送请求 $client = new Client(); $response = $client->post( "https://rcsbusinessmessaging.googleapis.com/v1/{$region}/phones/{$targetPhone}/messages", [ 'headers' => [ 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$accessToken}" ], 'json' => [ 'messageId' => $messageId, 'content' => ['text' => '这是一条测试RCS消息'], 'senderPhoneNumber' => $senderPhone, 'recipientPhoneNumber' => $targetPhone ] ] ); echo "消息发送成功:\n" . $response->getBody(); ?>
内容的提问来源于stack exchange,提问作者Tanzeem
相关产品推荐
相关产品推荐

