1

我正在尝试使用 REST 使用 php 写入 firebase,但我一直被拒绝权限

我正在使用这样的测试文件:

$url = 'https://my.firebase.url/';
$atoken = '?auth=MY FIREBASE SECRET';

$fb = new fireBase($url);

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1
        );

$todoPath = '/test/test';

printf("Database: %s\n", $url);

printf("Sending data to %s\n", $todoPath);
$response = $fb->set($todoPath, $todos.$atoken);
printf("Result: %s\n", $response);

printf("Reading data from %s\n", $todoPath);
$response = $fb->get($todoPath);
printf("Result: %s\n", $response);

我设置了规则,只有特定的授权用户“我”管理员可以写入火力库,但任何人都可以从中读取。当代码在 Javascript 中时这很好,因为我会登录并获得授权。但现在它在 php 中,我认为“秘密”可以完成这项工作,或者至少我被 firebase DOCS 引导相信这一点。

那么我做错了什么?

谢谢!

更新:

所以我改变了:

$atoken = '?auth=MY FIREBASE SECRET';

成为

$atoken = '.json?auth=MY FIREBASE SECRET';

我把它放在这里:

printf("Sending data to %s\n", $todoPath.$atoken);
    $response = $fb->set($todoPath.$atoken, $todos);
    printf("Result: %s\n", $response);

    printf("Reading data from %s\n", $todoPath.$atoken);
    $response = $fb->get($todoPath.$atoken);
    printf("Result: %s\n", $response);

现在我得到这个错误:

04Database: https://MYDATABASE/ Sending data to /test/test.json?auth=MY FIREBASE SECRET Result: { "error" : "invalid_token: Could not parse auth token." } Reading data from /test/test.json?auth=MY FIREBASE SECRET Result: { "error" : "invalid_token: Could not parse auth token." } 
4

2 回答 2

3

看起来非官方的 PHP / Firebase 库目前不支持身份验证,(请参阅https://github.com/ktamas77/firebase-php/issues/1stackoverflow.com/questions/15953505/firebase-php-curl-authentication /15953974

我建议分叉项目并添加一个.auth挂在您的令牌上的方法,并在添加 https://github.com/ktamas77/firebase-php/blob/master/firebaseLib.php# 后自动将其添加到每个.json请求中L63。不要忘记提交一个拉取请求来为其他人改进该库!

于 2013-05-02T18:21:30.860 回答
1

REST 规范看起来允许这样做。我和你读的一样。

你得到的确切错误是什么?您是否在 Forge 的模拟器中验证了您可以执行这些精确的操作?

至于您的 PHP 代码,有一件事很突出:

你有$response = $fb->set($todoPath, $todos.$atoken);

您将 $todos(一个数组)与 $atoken(一个字符串)连接起来,这是行不通的。您的意思是使用http_build_query还是json_encode

查看firebase-php库,无论如何您都不应该将它们变成字符串;看起来你应该这样做:

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1,
        'auth' => MY_FIREBASE_SECRET
        );
$response = $fb->set($todoPath, $todos);

或者可能

$todos = array(
        'name' => 'Pick the milk',
        'priority' => 1
        );
$response = $fb->set($todoPath.$atoken, $todos);
于 2013-05-01T22:55:34.157 回答