firebase: “Could not parse auth token.”

2020-06-06 00:52发布

Hello i am using firebase with php and using this library.

I am getting this error."Could not parse auth token."

My auth token is correct because i have used same token with node.js also.

my code looks like this

       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); 

can you please tell me what exactly i am doing wrong. thanks.

2条回答
闹够了就滚
2楼-- · 2020-06-06 01:21

I'm using somethink like this and it works for me. without any library. simple.

// 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;
查看更多
孤傲高冷的网名
3楼-- · 2020-06-06 01:28

I found the following solution worked, if anyone would still want to use topic starter's library:

  1. Go to Firebase Console and select your project.
  2. Go to Project Settings (press the gear icon beside Overview in the upper left)
  3. Go to the Service Accounts tab
  4. Select Database Secrets, show and copy the database secret key for the library's const DEFAULT_TOKEN

enter image description here

查看更多
登录 后发表回答