php - 通过 FCM google-php-api HTTPv1 API 发送推送消息
问题描述
我正在尝试使用 Google PHP API 客户端https://github.com/googleapis/google-api-php-client/通过 Google HTTPv1 API 发送 FCM 推送通知。
require_once 'vendor/autoload.php'; //-- loading the google api client
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account/key/project-sfk28as8ff.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();
// Your Firebase project ID
$project = "push-test-5f923";
// Creates a notification for subscribers to the debug topic
$message = [
"message" => [
"token" => "cO5hrNMFKQI:APA91bFm.......6IYy1phlxIJx2ZNA1",
"notification" => [
"body" => "This is an FCM notification message!",
"title" => "FCM Message",
]
]
];
// Send the Push Notification - use $response to inspect success or errors
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
var_dump($response);
以下是实际响应:
object(GuzzleHttp\Psr7\Response)#61 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(9) "Forbidden" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(403) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["Vary"]=> array(3) { [0]=> string(8) "X-Origin" [1]=> string(7) "Referer" [2]=> string(22) "Origin,Accept-Encoding" } ["Content-Type"]=> array(1) { [0]=> string(31) "application/json; charset=UTF-8" } ["Date"]=> array(1) { [0]=> string(29) "Thu, 27 Sep 2018 13:24:52 GMT" } ["Server"]=> array(1) { [0]=> string(3) "ESF" } ["Cache-Control"]=> array(1) { [0]=> string(7) "private" } ["X-XSS-Protection"]=> array(1) { [0]=> string(13) "1; mode=block" } ["X-Frame-Options"]=> array(1) { [0]=> string(10) "SAMEORIGIN" } ["X-Content-Type-Options"]=> array(1) { [0]=> string(7) "nosniff" } ["Alt-Svc"]=> array(1) { [0]=> string(40) "quic=":443"; ma=2592000; v="44,43,39,35"" } ["Accept-Ranges"]=> array(1) { [0]=> string(4) "none" } ["Transfer-Encoding"]=> array(1) { [0]=> string(7) "chunked" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["vary"]=> string(4) "Vary" ["content-type"]=> string(12) "Content-Type" ["date"]=> string(4) "Date" ["server"]=> string(6) "Server" ["cache-control"]=> string(13) "Cache-Control" ["x-xss-protection"]=> string(16) "X-XSS-Protection" ["x-frame-options"]=> string(15) "X-Frame-Options" ["x-content-type-options"]=> string(22) "X-Content-Type-Options" ["alt-svc"]=> string(7) "Alt-Svc" ["accept-ranges"]=> string(13) "Accept-Ranges" ["transfer-encoding"]=> string(17) "Transfer-Encoding" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#49 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(31) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } }
我不断得到的响应基本上是 Forbidden 403。(我也可以在谷歌开发者控制台中看到它)
Firebase Cloud Messaging API 和 Google Cloud Messaging 一样在开发者控制台中启用(尽管它不相关)。现在我认为发生这种情况是因为我没有执行 oAuth(没有获取访问令牌),但我不确定如何获取它,因为 google-api-php-client 的文档不是很详细。
谁能告诉我google-api-php-client中有哪些功能可以获取一次性访问代码,然后可以使用该代码来获取 firebase 的访问令牌,以便我可以通过 cURL 进行发布?(如何为 Firebase 项目的服务帐户获取有效的 Oauth 2.0 令牌)
解决方案
终于想通了。谷歌开发者控制台[https://console.developers.google.com]在云服务方面的功能似乎有些局限。(或至少有关此类服务的文档已过时)
正确的做法是仅使用 Firebase 控制台[https://console.firebase.google.com]中直接提供的 API 凭据。
或者,如果您喜欢冒险,您可以使用 Google Cloud Console [https://console.cloud.google.com],只要您确保您创建的服务帐户具有此处的 Google 文档中提到的编辑者或所有者权限[https://firebase.google.com/docs/cloud-messaging/auth-server]。
我发现 Google Cloud Console 过于复杂,所以我建议坚持使用 Firebase 控制台。
推荐阅读
- pandas - 将存储在数据框中的百分位值应用于数组
- javascript - Material-UI createShallow 检索用于酶单元测试的组件失败
- python - 如何使用漂亮的汤 4 去除文本格式标签?
- mongodb - 错误:子进程中发生异常:ChecksumMismatchError: SHA256 mismatch
- javascript - 当有函数不使用'yield'或'call'或'put'之类的效果时,如何测试sagas?
- c - 来自指针的函数中的指针:声明不兼容
- if-statement - 有没有办法计算空白单元格,但前提是它们在具有值的单元格之间,在 Google 表格中?
- python - 如何根据需要将多个 csv 文件与 Python 合并?
- python - 如何在没有用户名/密码系统的 Django 中进行会话?
- azure - 为什么在 Azure DevOps 中部署时收到“InternalServerError”?