Using C2DM get 401 problem?

719 views Asked by At

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!!

1

There are 1 answers

0
Jolanda Verhoef On

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!