【问题标题】:firebase: "Could not parse auth token."firebase:“无法解析身份验证令牌。”
【发布时间】:2017-10-22 18:23:40
【问题描述】:

您好,我正在使用带有 php 的 firebase 并使用此 library

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

我的身份验证令牌是正确的,因为我也使用了与 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); 

你能告诉我我到底做错了什么吗? 谢谢。

【问题讨论】:

    标签: php firebase firebase-authentication


    【解决方案1】:

    我发现以下解决方案有效,如果有人仍想使用主题启动器的library

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

    【讨论】:

    • 数据库机密已贬值,您有使用 firebase admin sdk 的解决方案吗?
    【解决方案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;
    

    【讨论】:

    • 你能指导我一点..我应该如何在我的代码中使用它?因为它看起来像 curl 和所有的东西完全不同
    • 我已更改代码并添加了您应更改的位置和内容。这是非常简单的代码。
    • 我自己使用的这段代码运行良好。 (我现在成功了):)
    • 那行不通的人......我收到错误“[{“error”:“InvalidRegistration”}]}“”
    • 这个解决方案对我有用?。我认为您的问题现在出在您的设备编码中
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    相关资源
    最近更新 更多