【问题标题】:unauthorized in the gcm [duplicate]在gcm中未经授权[重复]
【发布时间】:2017-01-22 12:13:45
【问题描述】:

CURL --header "Authorization: key=XXXXXXXXX" --header "Content-Type: application/json" https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"XXXXXXXX\"]}"

这是我的要求, 我试图从我的 cmd 发送这个, 使用我在谷歌控制台中创建的 api 密钥,我的端点为registration_ids。

但这就是我得到的:

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

有什么想法吗? 谢谢 :)

【问题讨论】:

标签: google-chrome service-worker


【解决方案1】:

导航到https://console.developers.google.com/apis/credentials?project=&lt;project-name&gt;

然后选择您创建的服务器密钥。在页面底部,您可以添加列入白名单的 IP/范围。

【讨论】:

  • 我有 api 密钥而不是服务器密钥。我输入了我的 ip 的密钥限制,它仍然是同样的错误
  • 发送的JSON中payload字段为空?那里的更改没有立即同步,需要几分钟。
【解决方案2】:

您收到 401 Unauthorized 是因为 GCM 只接受来自基于 HTTPS 的主机的请求,而您的本地控制台不接受。

验证这一点的最简单方法(如果您还没有基于 HTTPS 的网站)是:

第 1 阶段 - 获取启用 HTTPS 的网站

  1. 在 CloudFlare 上创建帐户
  2. 将您域的 DNS 更改为 CloudFlare 的
  3. 在 CloudFlare 控制台中输入您服务器的 IP
  4. 启用免费 HTTPS(并禁用缓存 - 有时可能很棘手)

现在您网站的流量将通过 CloudFlare 重定向。 从您的客户端到 CloudFlare 的流量将被加密,而从 CloudFlare 到您的服务器的流量则不会。

虽然它不是 100% 安全的,但它可以保护最常见的攻击媒介 - 恶意互联网提供商等,并且只需很少的努力即可获得免费的服务人员就绪网站。

如果你想要自己的证书,可以使用Let's Encrypt

第 2 阶段 - 将 GCM 发送方放在您的 HTTPS 服务器上

我有 PHP 中的示例代码:

<?

function sendPushNotification($data, $ids)
{
    echo("<br><br><b>Sending notifications...</b>");
    // Insert real GCM API key from the Google APIs Console
    $apiKey = 'AIza...';
    // Set POST request body
    $post = array(
      'registration_ids'  => $ids,
      'data'              => $data,
    );
    // Set CURL request headers
    $headers = array(
      'Authorization: key=' . $apiKey,
      'Content-Type: application/json'
    );
    // Initialize curl handle
    $ch = curl_init();

    // Set URL to GCM push endpoint
    curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send');
    // Set request method to POST
    curl_setopt($ch, CURLOPT_POST, true);
    // Set custom request headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Get the response back as string instead of printing it
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set JSON post data
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    // Actually send the request
    $result = curl_exec($ch);
    // Handle errors
    if (curl_errno($ch))
    {
      echo 'GCM error: ' . curl_error($ch);
    }
    // Close curl handle
    curl_close($ch);
    // Debug GCM response
    echo $result;
}


$data = array('message' => 'JAAAREEEEK!');
// Please note that currently push payload is not supported. However if you're reading this answer from the future it might work

// The recipient registration tokens for this notification
// https://developer.android.com/google/gcm/
$ids = ['registrationid1','registrationid2']

// Send push notification via Google Cloud Messaging
sendPushNotification($data, $ids);

我遇到了完全相同的问题,这个解决方案解决了它。如果您还有任何问题,请给我留言。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2014-02-26
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多