firebase:“无法解析身份验证令牌。”

Man*_*roi 4 php firebase firebase-authentication

您好,我正在将Firebase与php一起使用并使用此

我收到此错误。“无法解析身份验证令牌。”

我的身份验证令牌是正确的,因为我也已将相同的令牌与node.js一起使用。

我的代码看起来像这样

       require "vendor/autoload.php";
       const DEFAULT_URL = 'https://xxxxxxxxx.firebaseio.com';
       const DEFAULT_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

       $firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN );  

        $data = [
        "Website_Name"=> $Website_Name,
        "Username" =>  $Username,
       "Password"=> $Password,
       "website_link"=> $website_link,
       "id"=> $id,
       ];



        $path = "per_users";
        $res = $firebase->push('/per_users', $data);
        // per_users is the name of the table
        var_dump($res); 
Run Code Online (Sandbox Code Playgroud)

你能告诉我我到底在做什么错。谢谢。

rai*_*r00 7

如果有人仍然想使用主题入门,我发现以下解决方案有效:

  1. 转到Firebase控制台,然后选择您的项目。
  2. 转到项目设置(按左上角概述旁边的齿轮图标)
  3. 转到服务帐户标签
  4. 选择数据库密钥,显示并复制该库的数据库密钥 const DEFAULT_TOKEN

在此处输入图片说明

  • 数据库机密已贬值您有使用 firebase admin sdk 的解决方案吗? (2认同)

MRu*_*ade 2

我正在使用类似这样的想法,它对我有用。没有任何图书馆。简单的。

// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'YOUR_FIREBASE_API_KEY' ); //<--- here comes your api key from firebase
$registrationIds = array( "devices firebasetoken here." ); //<--- here comes device token (firebase generated token.)
// prep the bundle
$msg = array(
    'body'  => "message text",
    'title'     => "message title",
    'vibrate'   => 1,
    'sound'     => 1,
);
$fields = array(
        'registration_ids'  => $registrationIds,
        'notification'      => $msg
    );

$headers = array(
        'Authorization: key=' . API_ACCESS_KEY,
        'Content-Type: application/json'
    );

 $ch = curl_init();
 curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
 curl_setopt( $ch,CURLOPT_POST, true );
 curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
 curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
 curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
 curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
 $result = curl_exec($ch );
 curl_close( $ch );
 echo $result;
Run Code Online (Sandbox Code Playgroud)