通过FCM google-php-api HTTPv1 API发送推送消息

时间:2018-09-27 13:39:35

标签: php firebase firebase-cloud-messaging google-oauth google-api-php-client

我正在尝试使用Google PHP API客户端https://github.com/googleapis/google-api-php-client/通过Google HTTPv1 API发送FCM推送通知。

object(GuzzleHttp\Psr7\Response)#61 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(9) "Forbidden" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(403) ["headers":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["Vary"]=> array(3) { [0]=> string(8) "X-Origin" [1]=> string(7) "Referer" [2]=> string(22) "Origin,Accept-Encoding" } ["Content-Type"]=> array(1) { [0]=> string(31) "application/json; charset=UTF-8" } ["Date"]=> array(1) { [0]=> string(29) "Thu, 27 Sep 2018 13:24:52 GMT" } ["Server"]=> array(1) { [0]=> string(3) "ESF" } ["Cache-Control"]=> array(1) { [0]=> string(7) "private" } ["X-XSS-Protection"]=> array(1) { [0]=> string(13) "1; mode=block" } ["X-Frame-Options"]=> array(1) { [0]=> string(10) "SAMEORIGIN" } ["X-Content-Type-Options"]=> array(1) { [0]=> string(7) "nosniff" } ["Alt-Svc"]=> array(1) { [0]=> string(40) "quic=":443"; ma=2592000; v="44,43,39,35"" } ["Accept-Ranges"]=> array(1) { [0]=> string(4) "none" } ["Transfer-Encoding"]=> array(1) { [0]=> string(7) "chunked" } } ["headerNames":"GuzzleHttp\Psr7\Response":private]=> array(11) { ["vary"]=> string(4) "Vary" ["content-type"]=> string(12) "Content-Type" ["date"]=> string(4) "Date" ["server"]=> string(6) "Server" ["cache-control"]=> string(13) "Cache-Control" ["x-xss-protection"]=> string(16) "X-XSS-Protection" ["x-frame-options"]=> string(15) "X-Frame-Options" ["x-content-type-options"]=> string(22) "X-Content-Type-Options" ["alt-svc"]=> string(7) "Alt-Svc" ["accept-ranges"]=> string(13) "Accept-Ranges" ["transfer-encoding"]=> string(17) "Transfer-Encoding" } ["protocol":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#49 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(31) of type (stream) ["size":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["readable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["writable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["uri":"GuzzleHttp\Psr7\Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=> array(0) { } } }

这是实际答复:

from lxml.html import fromstring
#### Example HTML ####
text = '''
    <h4>Text</h4>
    <p>These is first Paragraph</p>
    <p>These is second Paragraph</p>
    <h4>title</h4>
    <p>casting of lxml element in xpath expression</p>
    <h4>Conclusion</h4>
    <p>Can any one give me a solution</p>
'''
tree = fromstring(text)
for i in tree.xpath('//h4'):
    ### getting the next h4 element####
    next_h4 = i.xpath('./following-sibling::h4[1]')
    #### need to get the text##
    text = i.xpath('./following-sibling::p[following-sibling::next_h4]//text()]')
    ## In the above xpath need to cast the lxml Element ##

我一直收到的响应基本上是“禁止的403”。(我也可以在Google开发人员控制台中看到它)

在开发人员控制台中启用了Firebase Cloud Messaging API,在Google Cloud Messaging中也启用了该功能(尽管没有关系)。现在我发现发生这种情况是因为我没有执行oAuth(未获取访问令牌),但是由于google-api-php-client的文档不是很详细,所以我不确定如何获取它。

谁能告诉我 google-api-php-client 中提供了哪些功能来获取一次性访问代码,然后可以使用该功能获取 em>用于Firebase的访问令牌,这样我就可以通过cURL进行发布了? (如何为Firebase项目的服务帐户获取有效的Oauth 2.0令牌)

1 个答案:

答案 0 :(得分:0)

最后弄清楚了。似乎Google Developer Console [https://console.developers.google.com]在处理云服务方面受到了一定的限制。 (或至少有关此类服务的文档已过时)

正确的做法是仅使用Firebase控制台 [https://console.firebase.google.com]中直接提供的API凭据。

enter image description here

或者,如果您喜欢冒险,可以使用Google Cloud Console [https://console.cloud.google.com],只要确保您创建的服务帐户具有Google文档 [https://firebase.google.com/docs/cloud-messaging/auth-server]中提到的“编辑者”或“所有者”权限即可。

我发现Google Cloud Console过于复杂,因此建议您坚持使用Firebase Console。