GoogleCloudMessaging Unauthorized Error 401

非 Y 不嫁゛ 提交于 2019-12-13 03:56:17

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/20267372/googlecloudmessaging-unauthorized-error-401

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!