C2DM 服务器出现 PHP 401 错误

发布于 2024-10-16 06:45:37 字数 1961 浏览 3 评论 0原文

我现在正在构建自己的 C2DM 应用程序。我首先从一个小型 Android 应用程序开始测试推送功能。如果我只是在 shell 中使用正确的设置调用curl 命令,它就可以工作。

现在对于服务器部分,我想使用 PHP,但似乎我做错了什么,因为当我尝试向客户端发送消息时,我总是收到 401 错误消息。首先,代码由两部分组成。第一个curl请求请求服务器令牌。这有效我从谷歌得到了一个带有工作令牌的真实响应!

第二个curl请求以401错误消息结束。有什么想法我做错了吗?

  $post_params = array ( "Email" => $MY_GOOGLE_ACC, "Passwd" => $MY_GOOGLE_PWD, "accountType"=>"GOOGLE", "source=" . $MY_GOOGLE_SRC, "service=ac2dm" ); 

  $first = true;
  $data_msg = "";

  foreach ($post_params as $key => $value) { 
    if ($first)
      $first = false;
    else
      $data_msg .= "&";

    $data_msg .= urlencode($key) ."=". urlencode($value); 
  }

  $x = curl_init("https://www.google.com/accounts/ClientLogin"); 

  curl_setopt($x, CURLOPT_HEADER, 1); 
  curl_setopt($x, CURLOPT_POST, 1); 
  curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg); 
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1); 
  $data = curl_exec($x); 
  curl_close($x); 
  $response = $data;

  $authKey = trim(substr($response, 4+strpos($response, "SID=")));

  echo $authKey;
  $collapse_key = 'something';

  $post_params = array ( "registration_id" => $DEVICE_TOKEN, "collapse_key" => $collapse_key, "data.payload"=>"cakephp" ); 

  $first = true;
  $data_msg = "";

  foreach ($post_params as $key => $value) { 
    if ($first)
      $first = false;
    else
      $data_msg .= "&";

    $data_msg .= urlencode($key) ."=". urlencode($value); 
  }

  $size=strlen($data_msg); 


  $x = curl_init("https://android.apis.google.com/c2dm/send"); 
  curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Content-Length:'. $size, 'Authorization: GoogleLogin auth=' . $authKey)); 
  curl_setopt($x, CURLOPT_HEADER, 1); 
  curl_setopt($x, CURLOPT_POST, 1); 
  curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg); 
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1); 
  $data = curl_exec($x); 
  curl_close($x); 
  $response = $data;

I'm building my own C2DM application right now. I first started with a small Android application to test the push feature. And it works if I just call the curl command with the correct settings in my shell.

Now for the server part I wanted to use PHP but as it seems I'm doing something wrong as I always get a 401 error message when I try to send a message to the client. First of all the code consists of two parts. The first curl request asks for the server token. This works I get a real response from google with a working token!

The second curl request ends up with a 401 error message. Any ideas what I'm doing wrong?

  $post_params = array ( "Email" => $MY_GOOGLE_ACC, "Passwd" => $MY_GOOGLE_PWD, "accountType"=>"GOOGLE", "source=" . $MY_GOOGLE_SRC, "service=ac2dm" ); 

  $first = true;
  $data_msg = "";

  foreach ($post_params as $key => $value) { 
    if ($first)
      $first = false;
    else
      $data_msg .= "&";

    $data_msg .= urlencode($key) ."=". urlencode($value); 
  }

  $x = curl_init("https://www.google.com/accounts/ClientLogin"); 

  curl_setopt($x, CURLOPT_HEADER, 1); 
  curl_setopt($x, CURLOPT_POST, 1); 
  curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg); 
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1); 
  $data = curl_exec($x); 
  curl_close($x); 
  $response = $data;

  $authKey = trim(substr($response, 4+strpos($response, "SID=")));

  echo $authKey;
  $collapse_key = 'something';

  $post_params = array ( "registration_id" => $DEVICE_TOKEN, "collapse_key" => $collapse_key, "data.payload"=>"cakephp" ); 

  $first = true;
  $data_msg = "";

  foreach ($post_params as $key => $value) { 
    if ($first)
      $first = false;
    else
      $data_msg .= "&";

    $data_msg .= urlencode($key) ."=". urlencode($value); 
  }

  $size=strlen($data_msg); 


  $x = curl_init("https://android.apis.google.com/c2dm/send"); 
  curl_setopt($x, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Content-Length:'. $size, 'Authorization: GoogleLogin auth=' . $authKey)); 
  curl_setopt($x, CURLOPT_HEADER, 1); 
  curl_setopt($x, CURLOPT_POST, 1); 
  curl_setopt($x, CURLOPT_POSTFIELDS, $data_msg); 
  curl_setopt($x, CURLOPT_RETURNTRANSFER, 1); 
  $data = curl_exec($x); 
  curl_close($x); 
  $response = $data;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远昼 2024-10-23 06:45:37

与curl 一起使用的键数组示例。这几乎是我正在工作的确切代码(为了清楚起见,进行了一些细微的更改)。

$headers = array('Authorization: GoogleLogin auth=' . $authcode);
$data = array(
    'registration_id' => $device_registration_id,
    'collapse_key' => 'ck_' . $device_id,
    'data.arg' => 'arrrrghhh'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

$authcode 是 ClientLogin 返回的 SID。 $device_registration_id 是我们执行 C2DM_REGISTER 时手机上的客户端应用程序给我们的注册 ID。

希望有帮助。

Example of key'd array use with curl. This is pretty much the exact code I have working (with minor changes for clarity).

$headers = array('Authorization: GoogleLogin auth=' . $authcode);
$data = array(
    'registration_id' => $device_registration_id,
    'collapse_key' => 'ck_' . $device_id,
    'data.arg' => 'arrrrghhh'
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if($headers) curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

$authcode is the SID returned by ClientLogin. $device_registration_id is the registration ID that the client app on the phone gave us when we did the C2DM_REGISTER.

Hope that helps.

北城半夏 2024-10-23 06:45:37

尝试在此处注册您的应用。我也有同样的问题。

Try to register your app here. I had the same problem.

染火枫林 2024-10-23 06:45:37

我不完全确定这里发生了什么,但是我从我的应用程序服务器中注意到一些工作正常的差异。

在对 CURLOPT_HTTPHEADER 的curl_setopt 调用中,内容长度和大小之间没有空格。这应该不会造成问题,但我以前见过网络服务器对类似的愚蠢事情变得脾气暴躁。

另外,CURLOPT_POSTFIELDS 是一个字符串,而我将我的作为键数组发送。

除此之外,我看起来与我的工作代码相同。

I'm not totally sure what's going on here, but here's a few difference I noticed from my application server that works fine.

In your curl_setopt call for CURLOPT_HTTPHEADER there is no space between the content length and the size. This 'shouldn't cause a problem, but I've seen webservers get tempremental over stupid stuff like that before.

Also, CURLOPT_POSTFIELDS is a string whereas I send mine as a key'd array.

Other than that I looks the same as my working code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文