Android Google GCM 服务器返回未经授权的错误 401
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12619759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google GCM server returns Unauthorized Error 401
提问by Sushil Kandola
I am using GCM services to push information from server. If I use browser key it shows the sucess mesaage as : {"multicast_id":4849013215736515938,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1348742583011905%2adac3a0f9fd7ecd"}]}but i did not get any notifications on device. And if I use server key it shows Unauthorized Error 401. My code is shown below:
我正在使用 GCM 服务从服务器推送信息。如果我使用浏览器键,它会显示成功消息:{"multicast_id":4849013215736515938,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0: 1348742583011905%2adac3a0f9fd7ecd"}]}但我没有在设备上收到任何通知。如果我使用服务器密钥,它会显示Unauthorized Error 401。我的代码如下所示:
$apiKey = "xxxxx";
$registrationIDs = array("xxxxxxxx");
$message = "testing Process";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array("message"=>$message),
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
curl_close($ch);
echo $result;
Please help me for this issue. Thanks in advance.
请帮我解决这个问题。提前致谢。
回答by Jos
Did you whitelist the IP of your server? This is not necessary by default for the browser key, but it is for the server key.
您是否将服务器的 IP 列入白名单?默认情况下,这对于浏览器密钥不是必需的,但对于服务器密钥是必需的。
You can check it here:
你可以在这里查看:
https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access
https://code.google.com/apis/console/#project:[您的项目编号]:访问
回答by DanO
Another answer already suggested whitelisting the IP address, which seems kind of obvious. What fixed it for me was whitelisting my IPv6 address. That was the key! I hope this helps someone else.
另一个答案已经建议将 IP 地址列入白名单,这似乎很明显。为我修复的是将我的IPv6 地址列入白名单。那是关键!我希望这对其他人有帮助。
回答by Dan Bartlett
I tried everything in this thread, and still no luck.
我尝试了这个线程中的所有内容,但仍然没有运气。
So I checked enabled APIs (APIs and Auth -> APIs, Enabled APIs), and realised that I had enabled Google Cloud Messaging for Chrome
not Google Cloud Messaging for Android
. As soon as I enabled the latter, it worked immediately.
所以,我检查启用的API(API和验证- >的API,启用的API),并意识到,我已经启用了Google Cloud Messaging for Chrome
没有Google Cloud Messaging for Android
。一旦我启用后者,它立即起作用。
Double check you have the right API enabled!
仔细检查您是否启用了正确的 API!
回答by Thomas Decaux
As many people wrote, you have to whitelist your server IPV4 and IPV6. If you want only IPV4, add this to your curl php init:
正如许多人所写,您必须将服务器 IPV4 和 IPV6 列入白名单。如果您只需要 IPV4,请将其添加到您的 curl php init 中:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
回答by mayur bhagat
use browser key using option create new browser key and use this key in your code. https://code.google.com/apis/console/?pli=1#project:42053154xxxx:access
使用浏览器密钥使用选项创建新的浏览器密钥并在您的代码中使用此密钥。 https://code.google.com/apis/console/?pli=1#project:42053154xxxx:access
回答by m1uan
I used to put to the
我曾经把
$apiKey = "xxxxx"
like project number (project id) which i put to Android Client app, but i have been wrong in server have to be appkey from google cloud console, (where you activate Google Cloud Messaging for Android)
就像我放到 Android 客户端应用程序中的项目编号(项目 ID)一样,但我在服务器中错误必须是来自谷歌云控制台的 appkey,(您在其中激活了适用于 Android 的谷歌云消息传递)
ProjectXXX -> APIs & auth -> Registered Apps -> Web App -> Server Key
-> Api key
in my case i had there default project 'Service Account-project' on platform: 'Web' but there was just Hosted Application section
and no evidence about any api key.
but when i create new Web Apps
called 'My app' which contained 4 sections OAuth 2.0 Client ID
, Certificate
, Server Key, Browser Key
i finaly find the api key;-)
在我的情况下,我在平台上有默认项目“服务帐户项目”:“Web”,但Hosted Application section
没有关于任何 api 密钥的证据。但是,当我创建新的Web Apps
名为“我的应用”,这包含4个部分OAuth 2.0 Client ID
,Certificate
,服务器密钥,Browser Key
我finaly找到API密钥;-)
may just me who don't know it but i believe this answer could also helps to some one
可能只有我不知道,但我相信这个答案也可以帮助某人
回答by Nir
for me the problem was that you have to enable the api. Having an API set up is not enough. Check that Google Cloud Messaging for Android appears on the enabled APIs in APIs-> enabled APIs.
对我来说,问题是你必须启用 api。仅设置 API 是不够的。检查 Google Cloud Messaging for Android 是否出现在 APIs-> enabled APIs 中的已启用 API 上。
If its not click the API library tab in APIS and enable it. \
如果不是,请单击 APIS 中的 API 库选项卡并启用它。\
回答by Pavan Pyati
You must have different project than the default one. Create a project & then create a key. Don't use default project.
您必须拥有与默认项目不同的项目。创建一个项目,然后创建一个密钥。不要使用默认项目。
回答by Nate Hammond
Had this trouble, I was using GCM (Google cloud messaging). But after Sep 2016, the server key on the GCM will not work, you have to use FCM (firebase cloud messaging). Create new server keys only in the Firebase Console using the Cloud Messaging tab of the Settings panel. I went over to the firebase console console.firebase.google.com (I had not used it yet) and it asked me to import my project. Suddenly there was this new server key back on the GCM console. If using GCM, use the "legacy key" you see listed there.
遇到了这个麻烦,我正在使用 GCM(谷歌云消息传递)。但是在 2016 年 9 月之后,GCM 上的服务器密钥将无法使用,您必须使用 FCM(firebase 云消息传递)。仅使用“设置”面板的“云消息传递”选项卡在 Firebase 控制台中创建新的服务器密钥。我转到 firebase 控制台 console.firebase.google.com(我还没有使用它),它要求我导入我的项目。GCM 控制台上突然出现了这个新的服务器密钥。如果使用 GCM,请使用您在此处看到的“旧密钥”。
回答by Qin Zhengquan
Here is the problem: I was using 2 servers, 1 staging and the other the production server. For my staging server, which was hosted using digital ocean, I entered the inet address in the Key for server applicationsallowed IP addresses, and it worked just fine.
问题是:我使用了 2 台服务器,1 台登台,另一台是生产服务器。对于使用数字海洋托管的临时服务器,我在服务器应用程序允许的 IP 地址的密钥中输入了 inet 地址,并且它工作得很好。
However, it failed when I changed to my inet address from my production server. Instead, I had to use inet6 addr: /64 Scope:Global for this to work. To get the value if you encounter the same issue, just enter ifconfig, and find the above entry. Enter that value in the allowed IP addresses and it will work fine.
但是,当我从生产服务器更改为我的 inet 地址时,它失败了。相反,我必须使用 inet6 addr: /64 Scope:Global 才能工作。要在遇到相同问题时获取该值,只需输入ifconfig,然后找到上述条目。在允许的 IP 地址中输入该值,它会正常工作。