提问者:小点点

通过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) { } } }

我一直得到的反应基本上是禁止的。(我也可以在谷歌开发者控制台中看到)

Firebase Cloud Messaging API与Google Cloud Messaging一样在开发人员控制台中启用(尽管不相关)。现在我想这是因为我没有做oAuth(没有获取访问令牌),但是我不确定如何获取它,因为GoogleAPI php客户端的文档不是很详细。

有人能告诉我google api php客户端中有哪些函数可以获取一次性访问代码,然后可以使用这些代码获取firebase的访问令牌,这样我就可以通过cURL进行发布了?(如何为Firebase项目的服务帐户获取有效的Oauth 2.0令牌)


共1个答案

匿名用户

我终于明白了。它似乎是谷歌开发者控制台[https://console.developers.google.com]在云服务方面,它所能做的有些有限。(或至少有关此类服务的文件已过时)

正确的做法是直接使用Firebase控制台中提供的API凭据[https://console.firebase.google.com].

或者,如果你喜欢冒险,你可以使用谷歌云控制台[https://console.cloud.google.com]只要您确保您创建的服务帐户具有Google文档中提到的编辑器或所有者权限[https://firebase.google.com/docs/cloud-messaging/auth-server].

我发现谷歌云控制台过于复杂,所以我建议坚持使用Firebase控制台。