简体   繁体   中英

GoogleCloudMessaging Unauthorized Error 401

I try to create project with use GoogleCloudMessaging to recieve push - notifications from my remote server. For client i use google tutorial Implementing GCM Client. For server i use this realization on php, but after i started php script, i got an error:

Unauthorized

Error 401

Here is my php code:

$url = 'https://android.googleapis.com/gcm/send';
$serverApiKey = "778688687533"; // sender id (Project Number)
$reg = "APA91bFFWLYsX8TK_XiGQJeUkHb_7FJZwZBSdnNEdxzQEQ-ijpmVvwBPxaz3wQPmWy_K6YAIYt1x_3IBDzbyALxcpmjL8mKEX7g5CV9QpXod3oFp4Hm5HqsY8snoQdua3l0NcMoHm0WE4oTbNUYcbAOE7zjJEV_p9Q"; // 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);
if ($headers)
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);

在此处输入图片说明在此处输入图片说明

To create the new server key, follow the steps,

  • Go to https://cloud.google.com/console/project
  • Click your Project
  • Go to APIs & auth on Left Panel
  • Select Credentials in submenu on Left Panel
  • Under Public API access , click on " Create New Key "
  • Choose ' Server '
  • On next dialog, Specify the whitelisted IPs, if you have any. Leaving it blank, the requests will be from any IP
  • Click Create .
  • You will find the new server key is created under " Public API Access "

This key is to be used for sending message in your script on server side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM