rds*_*rds 7 php rest firebase firebase-cloud-messaging
为了将HTTP V1 API(而不是旧版 API)与 PHP 一起使用,必须使用 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 代码)?
相关问题:一旦收到这个短暂的令牌,如何刷新这个令牌?工作流程是怎样的?
多谢!
如果您想手动获取访问令牌而不需要外部库,则可以使用此代码。它使用您的私钥创建 JWT 令牌,并请求不记名令牌。
function base64UrlEncode($text)
{
return str_replace(
['+', '/', '='],
['-', '_', ''],
base64_encode($text)
);
}
// Read service account details
$authConfigString = file_get_contents("path_to_your_private_key_file_downloaded_from_firebase_console.json");
// Parse service account details
$authConfig = json_decode($authConfigString);
// Read private key from service account details
$secret = openssl_get_privatekey($authConfig->private_key);
// Create the token header
$header = json_encode([
'typ' => 'JWT',
'alg' => 'RS256'
]);
// Get seconds since 1 January 1970
$time = time();
$payload = json_encode([
"iss" => $authConfig->client_email,
"scope" => "https://www.googleapis.com/auth/firebase.messaging",
"aud" => "https://oauth2.googleapis.com/token",
"exp" => $time + 3600,
"iat" => $time
]);
// Encode Header
$base64UrlHeader = base64UrlEncode($header);
// Encode Payload
$base64UrlPayload = base64UrlEncode($payload);
// Create Signature Hash
$result = openssl_sign($base64UrlHeader . "." . $base64UrlPayload, $signature, $secret, OPENSSL_ALGO_SHA256);
// Encode Signature to Base64Url String
$base64UrlSignature = base64UrlEncode($signature);
// Create JWT
$jwt = $base64UrlHeader . "." . $base64UrlPayload . "." . $base64UrlSignature;
//-----Request token------
$options = array('http' => array(
'method' => 'POST',
'content' => 'grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion='.$jwt,
'header' =>
"Content-Type: application/x-www-form-urlencoded"
));
$context = stream_context_create($options);
$responseText = file_get_contents("https://oauth2.googleapis.com/token", false, $context);
$response = json_decode($responseText);
Run Code Online (Sandbox Code Playgroud)
响应有 3 个字段:access_token、expires_in和token_type。
您应该将令牌存储在某个地方以供将来使用,并在令牌过期时根据expires_in. (1小时后)。
您还可以请求更短生命周期的令牌,但令牌的最长生命周期为 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存储库的 README 中,您可以找到有关如何获取 OAuth 访问和刷新令牌的说明。
这两个库都在底层与 Guzzle 配合使用,并提供了一种使用授权中间件来装饰您自己的 Guzzle HTTP 客户端的方法,这样您就不必这样做。
因此,如果其中一个库不提供对您想要访问的 API 的支持,您可以应用以下代码片段中的代码并自行访问相关 API(来自Google Api PHP 客户端 - “直接发出 HTTP 请求”) :
// 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();
Run Code Online (Sandbox Code Playgroud)
无耻插件:我正在维护一个单独的管理 SDK,用于访问https://github.com/kreait/firebase-php上的 Firebase 相关 API ,并且它有一个 FCM 组件,记录在此处:https://firebase-php。 readthedocs.io/en/stable/cloud-messaging.html
| 归档时间: |
|
| 查看次数: |
5147 次 |
| 最近记录: |