【问题标题】:Firebase Cloud Messaging HTTP V1 API: How to Get the Auth 2.0 Access Token with REST Calls?Firebase 云消息传递 HTTP V1 API:如何通过 REST 调用获取 Auth 2.0 访问令牌?
【发布时间】:2018-03-14 08:02:06
【问题描述】:

为了在 PHP 中使用 HTTP V1 API(不是旧版 API),必须使用 REST 接口。

https://firebase.google.com/docs/cloud-messaging/send-message#top_of_page

我想知道如何获取 Auth 2.0 访问令牌?

https://firebase.google.com/docs/cloud-messaging/auth-server

由于 PHP 没有 Google API Client Library(参见上面链接中的示例),如何通过 REST 调用接收 Auth 2.0 令牌(无需显示 PHP 代码)?

相关问题:一旦收到这个短暂的token,如何刷新这个token?工作流程是什么?

非常感谢!

【问题讨论】:

标签: php rest firebase firebase-cloud-messaging


【解决方案1】:

其实有一种 PHP 的“Google Api 客户端库”,甚至有两个:

https://github.com/google/google-api-php-client

https://github.com/GoogleCloudPlatform/google-cloud-php

一个提供对 API 的访问而另一个不提供,因此值得看看哪个提供什么 - 您可能需要同时使用它们。

https://github.com/google/google-api-php-client 存储库的自述文件中,您可以找到有关如何获取 OAuth 访问和刷新令牌的说明。

这两个库都与 Guzzle 一起工作,并提供了一种使用授权中间件装饰您自己的 Guzzle HTTP 客户端的方法,这样您就不必这样做了。

因此,如果其中一个库不支持您要访问的 API,您可以应用以下 sn-p 中的代码并自己访问相关 API(来自 Google Api PHP Client - "Making HTTP requests directly"):

// create the Google client
$client = new Google_Client();

/**
 * Set your method for authentication. Depending on the API, This could be
 * directly with an access token, API key, or (recommended) using
 * Application Default Credentials.
 */
$client->useApplicationDefaultCredentials();

// returns a Guzzle HTTP Client
$httpClient = $client->authorize();

无耻插件:我在 https://github.com/kreait/firebase-php 维护一个单独的 Admin SDK 用于访问与 Firebase 相关的 API,它有一个 FCM 组件,记录在这里:https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2018-06-25
    • 2022-08-19
    • 2019-07-19
    • 2018-03-24
    • 2021-02-18
    • 2016-11-21
    相关资源
    最近更新 更多