Firebase云消息传递HTTP V1 API:如何通过REST调用获取Auth 2.0访问令牌?

7
为了在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客户端库(请参见上面链接中的示例),因此如何使用REST调用接收Auth 2.0令牌(无需显示PHP代码)?
相关问题:一旦收到这个短期令牌,如何刷新该令牌? 工作流程是什么?
非常感谢!

请查看此答案:https://dev59.com/XlUL5IYBdhLWcg3wzavS#61871906 - lubilis
这是有关获取OAuth Bearer令牌的REST调用的文档:https://developers.google.com/identity/protocols/oauth2/service-account#httprest - Peter Bruins
2个回答

9
如果您想手动获取访问令牌而不使用外部库,可以使用以下代码。它使用您的私钥创建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);

响应有3个字段:access_tokenexpires_intoken_type
您应该将您的令牌存储在某个地方以备将来使用,并在基于expires_in的到期时请求新令牌(1小时后)。
您还可以请求寿命较短的令牌,但是令牌的最长寿命为1小时。

它基于此文档:https://developers.google.com/identity/protocols/oauth2/service-account#httprest - Peter Bruins
谢谢。这就是我正在寻找的。 - Sos.

4

实际上,有两种针对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 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();

不要介意我做个自我推销:我正在维护一个单独的Admin SDK,用于访问Firebase相关API,网址为https://github.com/kreait/firebase-php。它还有一个FCM组件,这里有文档:https://firebase-php.readthedocs.io/en/stable/cloud-messaging.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接