0

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);

enter image description hereenter image description here

4
  • You have to use server key! Nov 28, 2013 at 13:26
  • Do i have to use server key instead of project number? where can i get/create server key? google has changed disign of console, and i can't find server key. Thanks!
    – Denis
    Nov 28, 2013 at 13:50
  • 1
    I solved the problem. As Sunil Mishra wrote i had to use server key instead of sender id (Project Number) in my php script.
    – Denis
    Nov 28, 2013 at 19:53
  • what do you mean "server key" ? I have the same issue, thanks Jan 19, 2014 at 17:49

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.