我试图用php使用REST写一个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);
我设置了规则,只有一个特定的授权用户"我"管理员可以写到firebase,但任何人都可以从里面读。当代码是Javascript时,这很好,因为我将登录并获得授权。但是现在它在php中,我认为"SECRET"会使它工作,或者至少我被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." }
看起来非官方的PHP/Firebase库目前不支持身份验证,(参见https://github.com/ktamas77/firebase-php/issues/1和stackoverflow.com/questions/15953505/firebase-php-curl-authentication/15953974。
我建议将项目分叉并添加一个.auth
方法,该方法挂载在令牌上,并在https://github.com/ktamas77/firebase-php/blob/master/firebaseLib.php#L63上添加.json
后自动将其添加到每个请求中。不要忘记提交一个pull request来为其他人改进这个库!
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);