Làm thế nào để add items vào google base

Events happening in the community are now at Drupal community events on www.drupal.org.
dothaiduong2002's picture

Chào mọi người, mình là một thành viên mới của group này, và cũng là một newbie drupal và mình gặp một rắc rối trong việc submit item tự động lên google base.
Mình đã login thành công và lấy được token của nó, nhưng ko biết biết làm sao để sử dụng token đó để lấy các item về, hay insert, update, delete.

<?php
function googlebaseapi_get_token($admin_email='', $admin_password='') {
 
   
$post_data = 'accountType=HOSTED_OR_GOOGLE&Email='. urlencode($admin_email) .'&Passwd='. urlencode($admin_password). '&service=gbase';
 
$result = drupal_http_request("https://www.base.google.com/accounts/ClientLogin",
                    array(
"Content-Type" => "application/x-www-form-urlencoded"),
                     
'POST',
                     
$post_data);
  if(
is_object($result) &&  preg_match_all('/^(\S+)=(\S+)/m', $result->data, $matches, PREG_SET_ORDER) ) {
   
$temp_token = array();
    foreach(
$matches as $set)
     
$temp_token[$set[1]] = $set[2];
   
$object_token = (object)$temp_token;
   
$token = $object_token->Auth;
    return
$token;
  }
  else {
    
watchdog('googleappsapi', t('The token from Google seems to be broken.'), WATCHDOG_WARNING);
      
drupal_set_message(t('The token from Google seems to be broken.'), 'error');
       return
FALSE;
  }
}

function
googlebaseapi_getItems($token) {

/* Our Google Base API developer key. You are register on google to get this key (http://code.google.com/apis/base/signup.html) <em>/
$developerKey = "paste_this_here";

/</em> The items feed URL, used for queries, insertions and batch commands. */
$itemsFeedURL = "http://www.google.com/base/feeds/items";

$result = drupal_http_request($itemsFeedURL, array(
                                                              
'Authorization: AuthSub token='. $token,
                                                            
'Content-Type' => 'application/atom+xml',
                                                              
'X-Google-Key: key='. $developerKey));
 
   
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
   
xml_set_character_data_handler($xml_parser, "characterData");
  
xml_parse($xml_parser, $result->data);
 
xml_parser_free($xml_parser);
 
//Test
 
echo $result->data;
 
/**Output is:
  * Authorization required
  * Error 401
  */
}
?>

Mọi người ai đã làm về cái này rồi xin hãy cho ý kiến nhé.
Thank all.

Comments

Code viết tè le không

thehong's picture
  • Code viết tè le không theo chuẩn nào cả.
  • Code không có comment, người khác đọc không tài nào hiểu nổi.

Vui lòng chỉnh sửa lại.

--
Thế Hồng

Nhìn code thì cũng khó hiểu

luatviettin's picture

Nhìn code thì cũng khó hiểu thật, nhưng nên nhớ là google có chuẩn riêng, chỉ cần lệch chuẩn một tí là nó báo lỗi luôn, chống hack cực chuẩn, code này của bạn hình như chưa được chuẩn khi dùng drupal_http_request thì phải ! Mình thì dạo gần đây thường lấy của bọn wordpress cả một cái class dài lê thê dùng tốt hơn của Drupal ! nó là wp_remote_post();
Bạn thử cái đấy xem !