1

我正在尝试向 Android 手机发送消息,但不断收到带有文本的响应代码 401:未经授权。此外,我一直在阅读关于使用什么密钥的不同故事,我知道 3 个密钥:项目 ID(编号)、服务器应用程序的密钥和浏览器应用程序的密钥。所以我尝试了所有 3 个,结果都一样。

我的代码:

$headers = array("Content-Type" => "application/json", "Authorization" => "key=" . "mykey");
    $data = array(
        'data' => $messageText,
        'registration_ids' => array($deviceRegistrationId)
    );

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send");
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    error_log(json_encode($data));
    $response = curl_exec($ch);
    curl_close($ch);
    error_log($response);
4

2 回答 2

4

I changed the header to:

$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . "mykey");

And it works. The mykey is Key for browser apps.

于 2012-07-09T14:10:23.127 回答
0

你可以这样做你的标题只是为了让它更容易阅读并消除连接:

$headers = array(
    "Authorization:key=mykey",
    "Content-Type:application/json",
);
于 2012-07-18T17:39:37.617 回答