2011-02-17 69 views
1

嗨〜大家好! 我已經獲得了手機的registration_id以及來自Google的auth令牌,用於執行推送的我的gmail帳戶。使用C2DM獲得401問題?

但是,當我把我的身份驗證令牌和登記ID到Ubuntu並嘗試通過PHP,PHP的捲曲和捲曲僅將它張貼以請求C2DM得到的消息。我總是得到401未經授權的。

,這是我的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; 

難道我錯過或財產以後做什麼了嗎?

任何幫助,將不勝感激,謝謝!

回答

0

看來你是從響應,而不是驗證字段提取SID。

$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 

這是你正在尋找這最後一場!