【问题标题】:GCM Unauthorized with PHP AppEngineGCM 未经 PHP AppEngine 授权
【发布时间】:2015-03-12 14:04:00
【问题描述】:

我正在使用 Appengine 并使用测试脚本通过 GCM 发送消息,但它变成了 401 Unauthorized 错误。

跟随我的脚本

$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "AIzaSyCKPMazvtdyhqhH7IdIqdHjqUtLNlHnsgA"; // API KEY SERVER
$reg = "APA91bEmal_3NpDSV5vuIysMjF8GYxO70_2Nmzx78VCSLmGpX_oX73TqfHczzjP_PgUmwYcdGz0yPGAajRLb1eP7wfzqtoADMzL1-DoCLYH9kz2dKknwcd7fZFSg_qM6Cgk_gphIRWD2FbcJZ83-ymnF1UhF40j32vYzD7WCDZnq_0-87R5Dbmk"; // registration id

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

$data = array(
'registration_ids' => array($reg),
'data' => array(
'message' => 'Hello, World!'
));

print (json_encode($data) . "\n\n");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$response = curl_exec($ch);

curl_close($ch);

print ($response);

如果我在本地机器上做同样的测试,它可以工作,但在 appengine 上不工作

http://s3.postimg.org/ndzcfujoj/local_machine_1.png

http://s17.postimg.org/f48alja3z/local_machine_2.png

...在 appengine 中不起作用

{"registration_ids":["APA91bEmal_3NpDSV5vuIysMjF8GYxO70_2Nmzx78VCSLmGpX_oX73TqfHczzjP_PgUmwYcdGz0yPGAajRLb1eP7wfzqtoADMzL1-DoCLYH9kz2dKknwcd7fZFSg_qM6Cgk_gphIRWD2FbcJZ83-ymnF1UhF40j32vYzD7WCDZnq_0-87R5Dbmk"],"data":{"message":"Hello, World!"}}
Unauthorized
Error 401

遵循 API 设置

Key for server applications
API key
AIzaSyCKPMazvtdyhqhH7IdIqdHjqUtLNlHnsgA
IPs
Any IP allowed
Activation date
Mar 12, 2015, 2:40:00 AM

有人可以帮我吗?

【问题讨论】:

    标签: android google-app-engine google-cloud-messaging unauthorized


    【解决方案1】:

    尝试使用浏览器密钥。 服务器密钥来自公共 IP 地址。 此外,您应该将您的 IP 地址列入白名单。

    【讨论】:

    • 创建了浏览器 API,但它发生了同样的问题...这里是密钥浏览器 API.... 浏览器应用程序的密钥 API 密钥 AIzaSyBDLrlKSotjkf0u46veF_Y8JfOx0nTZIVU 推荐人 允许任何推荐人 激活日期 2015 年 3 月 12 日,4 :58:00 AM
    【解决方案2】:

    我不知道 cURL 不起作用的原因是什么。我用另一种方法做了,它奏效了

    $regId = "myid";
        $message = "Hello World!";
    
    
        $registrationIds = array($regId); 
        $msg = array( 'message' => $message, 'title' => 'notification center', 'tickerText' => $message, 'vibrate' => 1, 'sound' => 1, );
    
        $fields = array(
            'registration_ids' => $registrationIds,
            'data' => $msg
        );
        $fields = json_encode($fields);
        $arrContextOptions=array(
            "http" => array(
                "method" => "POST",
                "header" =>
                    'Authorization: key = MyApiKey'. "\r\n" .
                    'Content-Type: application/json'. "\r\n",
                "content" => $fields,
            ),
            "ssl"=>array(
                "allow_self_signed"=>true,
                "verify_peer"=>false,
            ),
        );
        $arrContextOptions = stream_context_create($arrContextOptions);
        $result = file_get_contents('https://android.googleapis.com/gcm/send', false, $arrContextOptions);
    
        echo $result;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 2017-01-22
      • 2014-02-26
      • 1970-01-01
      相关资源
      最近更新 更多