GoogleCloudMessaging未经授权的错误401 [英] GoogleCloudMessaging Unauthorized Error 401

查看:171
本文介绍了GoogleCloudMessaging未经授权的错误401的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用GoogleCloudMessaging创建项目,以接收来自远程服务器的推送通知。
对于客户我使用谷歌教程实施GCM客户端。
对于服务器我在php上使用这个实现,但是在我启动php脚本之后,我得到了一个错误:

未经授权



错误401



以下是我的php代码:

  $ url ='https://android.googleapis.com/gcm/send'; 
$ serverApiKey =778688687533; //发件人ID(项目编号)
$ REG = APA91bFFWLYsX8TK_XiGQJeUkHb_7FJZwZBSdnNEdxzQEQ-ijpmVvwBPxaz3wQPmWy_K6YAIYt1x_3IBDzbyALxcpmjL8mKEX7g5CV9QpXod3oFp4Hm5HqsY8snoQdua3l0NcMoHm0WE4oTbNUYcbAOE7zjJEV_p9Q; //注册ID

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

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

print(json_encode($ data)。\\\
\\\
);

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

解决方案

要创建新的服务器密钥,请按照以下步骤操作:


  • a href =https://cloud.google.com/console/project =nofollow> https://cloud.google.com/console/project

  • 点击您的项目

  • 转到 APIs&在左侧面板上选择授权

  • 在左侧面板上的子菜单中选择凭证

  • 公共API访问权限打开创建新密钥

  • 选择服务器指定白名单IP,如果有的话。将这些请求留空,请求将来自任何IP

  • 单击创建

  • 您将发现新的服务器密钥在公共API访问下创建



此密钥用于在脚本中发送消息在服务器端。


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.

这篇关于GoogleCloudMessaging未经授权的错误401的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆