2011-02-17 2 views
1

HI ~ tout le monde! J'ai reçu l'identifiant d'enregistrement du téléphone et un jeton d'authentification de google pour mon compte Gmail qui effectue un push.Utilisation C2DM obtenir 401 problème?

Mais quand j'envoie mon jeton d'authentification et mon identifiant d'enregistrement à Ubuntu et essaye de le poster par php, php's curl et seulement curl pour demander à C2DM d'obtenir un message. Et je reçois toujours la 401 non autorisée.

Et voici mon code 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; 

Suis-je manque somthing ou quelque chose de mal?

ET toute aide serait très appréciée, merci !!

Répondre

0

Il semble que vous extrayez le SID de la réponse, pas le champ Auth.

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

devrait être

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

Essayez d'imprimer toute la réponse, vous verrez quelque chose comme:

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

C'est ce dernier domaine que vous recherchez!

Questions connexes