使用C2DM出现401问题?

发布于 2024-10-17 16:09:51 字数 1963 浏览 1 评论 0原文

哈喽~大家好! 我已经从谷歌获得了手机的 Registration_id 和正在执行推送的 Gmail 帐户的身份验证令牌。

但是当我将我的身份验证令牌和注册ID发送到Ubuntu并尝试通过php发布它时,php的curl并且只有curl来请求C2DM以获取消息。我总是收到 401 Unauthorized。

这是我的 php 代码......

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

我错过了什么或做错了什么吗?

任何帮助将不胜感激,谢谢!

HI~everyone!
I have gotten the registration_id for the phone and an auth token from google for my gmail account that is performing a push.

But When I send my auth token and registration id to Ubuntu and try to post it by php , php's curl and only curl to request to C2DM to get a message. And I always get the 401 Unauthorized.

And this is my php code ....

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

Am I miss somthing or do something wrong?

AND any help would be much appreciated,thanks!!

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

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

发布评论

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

评论(1

蓦然回首 2024-10-24 16:09:52

您似乎是从响应中提取 SID,而不是 Auth 字段。

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

应该是

$authKey = trim(substr($response, 5+strpos($response, "Auth=")));

尝试打印整个响应,您会看到类似的内容:

SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
Auth=DQAAAGgA...dk3fA5N

这是您正在搜索的最后一个字段!

It seems you are extracting the SID from the response, not the Auth field.

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

should be

$authKey = trim(substr($response, 5+strpos($response, "Auth=")));

Try printing the whole response, you'll see something like:

SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
Auth=DQAAAGgA...dk3fA5N

It's this last field that you're searching for!

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