【问题标题】:Firebase custom token php server generation errorFirebase自定义令牌php服务器生成错误
【发布时间】:2017-04-25 17:13:00
【问题描述】:

我已经安装了 firebase 推荐的用于 php 自定义令牌的包,可以在此处找到,https://firebase.google.com/docs/auth/admin/create-custom-tokens 可以在 使用第三方 JWT 库创建自定义令牌标题下找到。

我已经安装了 php-jwt 建议。 然后,我创建了一个找到 here 的服务帐户,并在 /apis/credentials 中获得了与下载类似的详细信息。

"private_key": "-----BEGIN PRIVATE KEY-----veryLongKey---END PRIVATE KEY-----\n",
"client_email": "randomemail@appspot.gserviceaccount.com",

然后我使用顶部链接所示的格式生成令牌。

但是,当我将 $theCreatedToken 放入此卷曲时 ....

    $url = 'https://localhost-42d67.firebaseio.com/Devices.json?auth=' .$theCreatedToken;
    $arr = array("success" =>array("iPhone"=>500));
    $data_string = json_encode($arr);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
    );
echo $result = curl_exec($ch);

我收到此错误 { "error": "应为 ID 令牌,但获得了自定义令牌。" }

如果我删除 ?auth=' .$theCreatedToken; 它可以工作。 我究竟做错了什么?我对此进行了广泛的搜索,但没有答案。 我希望有人能够提供帮助。 最好的

【问题讨论】:

  • 我遵循了这个解决方案,但它也不起作用,同样的错误。 link
  • 你是如何获得 $theCreatedToken 的?
  • $theCreatedToken 从 JWT 库返回。

标签: php curl firebase jwt firebase-authentication


【解决方案1】:

这很令人困惑,但我通过使用这个库和在 Firebase 设置中生成的服务帐户 JSON 来实现它。

https://github.com/kreait/firebase-php

我还编写了这个库以使其与 Guzzle 5 和 PHP 5.6 一起使用

https://github.com/luqmanrom/firebase-php

显然,它在 Authorization 标头中使用 Bearer 令牌,而不是在查询字符串中传递 auth 令牌。令牌是从 Google OAuth2 服务器获取的。

希望对你有帮助

【讨论】:

  • 是的,它非常令人困惑。最后我和你一样:)
  • 我必须重写以使其与 Guzzle 5 一起使用。稍后将其作为公共包。如果这有帮助,请评价
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-24
  • 1970-01-01
  • 2016-11-10
  • 2018-08-18
  • 2016-09-25
  • 1970-01-01
  • 2017-03-22
相关资源
最近更新 更多