使用C2DM得到401问题?

时间:2011-02-17 01:47:44

标签: android push android-c2dm

HI〜大家好! 我已经获得了手机的registration_id和来自谷歌的身份验证令牌,用于执行推送的我的Gmail帐户。

但是当我将我的身份验证令牌和注册ID发送到Ubuntu并尝试通过php发布时,php的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; 

我是否想念某事或做错事?

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

1 个答案:

答案 0 :(得分:0)

您似乎从响应中提取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

这是你正在寻找的最后一个领域!