用PHP发送POST请求到pushbullet API 401错误

时间:2015-05-26 23:39:45

标签: php api post pushbullet

我正在尝试使用pushbullet发送简单的推送通知,只需使用电子邮件并将其发送到链接帐户以避免需要帐户数据。 (参见此处的参考:https://docs.pushbullet.com/#pushes

因此我在php中使用非cURL方法,我(不仅仅是)在这里找到: How do I send a POST request with PHP?

不幸的是我收到了如下错误:

<br />
<b>Warning</b>:  file_get_contents(https://api.pushbullet.com/v2/pushes): failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
 in <b>/path/to/function.php</b> on line <b>42</b><br />
bool(false)

将用于file_get_contents的网址的选项设置为“on”。

我的代码:

$pushdata = array(
    "email"     => $email,
    "type"      => "link",
    "title"     => "Demo Pushbullet Notification",
    "body"      => "You have new comment(s)!",
    "url"       => "http://demo.example.com/comments"
);

//Post without cURL

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n"."Authorization: Bearer <xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>\r\n",
        'method'  => 'POST',
        'content' => http_build_query($pushdata),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents("https://api.pushbullet.com/v2/pushes", false, $context, -1, 40000);
var_dump($result);
编辑:将代码改为christopherhesse的回复,仍然无法正常工作。它也不应该要求访问令牌,因为我理解推动。我理解为推送从中立到关联电子邮件的通知。也许我错了,但访问令牌无法修复它。

编辑(已解决):推送通知需要访问令牌 IS ,因为它不适用于此方法,它可以与cURL一起使用。

3 个答案:

答案 0 :(得分:1)

听起来您错过了用户帐户的访问令牌。您可以在https://www.pushbullet.com/account找到它。您应该将其包含在“授权”标题中:'Bearer ACCESS_TOKEN_HERE'。

答案 1 :(得分:1)

我知道这是一篇旧帖子,但我遇到了与使用相同的授权:Bearer APIKEY方法作为您的网站的sendgrid相同的问题。

我终于使用以下代码使用原始php帖子工作了

$url = "your url";
$post_data = 'yourdata';

// Set the headers
$options = array('http' =>
    array(
        'method'  => 'POST',
        'header' => "Authorization: Bearer APIKEY\r\n" . 'Content-Type: application/x-www-form-urlencoded',
        'content' => $post_data
    )
);

// Send the POST data
$ctx = stream_context_create($options);
$fp = fopen($url, 'rb', false, $ctx);
// Read the POST data
$result = json_decode(stream_get_contents($fp));
echo "done";

似乎切换标题的顺序并在结尾处省略\ r \ n似乎有效。我在这个尝试不同的组合上花了一个小时,所以我想我会把这个发给其他人。

注意,如果你使用sendgrid api,请确保设置'Content-Type:application / json'并确保$ post_data在json中

答案 2 :(得分:0)

您需要为此使用cURL,以便您可以将API密钥作为文档中定义的标头传递:https://docs.pushbullet.com/#http

<?php

$curl = curl_init('https://api.pushbullet.com/v2/pushes');

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer <your_access_token_here>']);
curl_setopt($curl, CURLOPT_POSTFIELDS, ["email" => $email, "type" => "link", "title" => "Demo Pushbullet Notification", "body" => "You have new comment(s)!", "url" => "http://demo.example.com/comments"]);

// UN-COMMENT TO BYPASS THE SSL VERIFICATION IF YOU DON'T HAVE THE CERT BUNDLE (NOT RECOMMENDED).
// curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);

print_r($response);

这条代码完全脱离了我的头脑,并且已经过测试

我已经破坏了选项,因此您可以轻松地看到它们,但您可以将它们组合成一个数组并通过curl_setoptarray($curl, []);传递它们