Android GCM服务器端PHP-未经授权的401错误
我设置了谷歌开发者控制台,为Android启用谷歌云消息 在凭证端,我创建浏览器API密钥,在引用中键入0.0.0.0。实际上,我创建了这两种类型的键,因为我在不同的教程中发现了不同的指示 我用这个PHP脚本测试了密钥Android GCM服务器端PHP-未经授权的401错误,android,push-notification,google-api,google-cloud-messaging,Android,Push Notification,Google Api,Google Cloud Messaging,我设置了谷歌开发者控制台,为Android启用谷歌云消息 在凭证端,我创建浏览器API密钥,在引用中键入0.0.0.0。实际上,我创建了这两种类型的键,因为我在不同的教程中发现了不同的指示 我用这个PHP脚本测试了密钥 <? /** * The following function will send a GCM notification using curl. * * @param $apiKey [string] The Browser API key stri
<?
/**
* The following function will send a GCM notification using curl.
*
* @param $apiKey [string] The Browser API key string for your GCM account
* @param $registrationIdsArray [array] An array of registration ids to send this notification to
* @param $messageData [array] An named array of data to send as the notification payload
*/
function sendNotification( $apiKey, $registrationIdsArray, $messageData )
{
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$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) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
?>
<?
// Message to send
$message = "the test message";
$tickerText = "ticker text message";
$contentTitle = "content title";
$contentText = "content body";
$registrationId = '372CBFD0C4BFE728';
$apiKey = "AIzaSyDeNN1XJBFGE_lJ_35VMUmx5cUbRCUGkjo";
$response = sendNotification(
$apiKey,
array($registrationId),
array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );
echo $response;
?>
但我(使用两个密钥)获得了未经授权的401错误
谢谢您的帮助。不要输入
0.0.0.0
作为允许的推荐人或允许的IP,不要输入任何内容。这应该行。不要在IP地址字段中输入任何内容。它就像一个符咒。
还要确保您在服务器端使用了服务器密钥和正确的发件人id android客户端。我也收到了相同的错误。您是如何解决这个问题的,我正在本地主机中进行测试,并得到unauthorized-401-error。你能在这里帮忙吗谢谢你给我指了这个:curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);没有这个,我就犯了一个错误
{"multicast_id":6782339717028231855,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]}