1

我正在尝试使用 Google Sites API 来检索我域的所有站点的列表,而不仅仅是当前经过身份验证的用户具有读写/所有者访问权限的站点。据我所知,在当前的 API 中没有处理这个问题的工具。

但是,根据当前的 API 参考(https://developers.google.com/google-apps/sites/docs/1.0/reference/),我可以使用一个参数来包含用户至少可以“查看”的所有站点. 这与使用域管理员用户相结合似乎可以满足我的要求。

不幸的是,我无法让它工作。我使用的是 2-legged oAuth 方法,与此处的以下示例几乎相同:http://gdatatips.blogspot.ca/search/label/oauth,除了我使用的是网站提要。

我可以成功指定 xoauth_requestor_id=my.admin.user@domain.com 并检索站点列表。但是,如果我将 'include-all-sites' 参数添加到我的提要 url,即:

https://sites.google.com/feeds/site/mydomain.com/?include-all-sites=true

我收到以下错误:

未知的授权标头错误 401

还有另一种方法可以做到这一点吗?我找不到任何使用“include-all-sites”参数的文档或工作示例。

更新:

这是标题输出:

GET /feeds/site/mydomain.com/?include-all-sites=true HTTP/1.1
Host: sites.google.com
Accept: */*
Authorization: OAuth oauth_version="1.0", oauth_nonce="831c013d3d4c9c13b149dfd21e9b48bd", oauth_timestamp="1337274463", oauth_consumer_key="mydomain.com", xoauth_requestor_id="user%mydomain.com", oauth_signature_method="HMAC-SHA1", oauth_signature="%2Bq24MoAQJL4vCcz%2BLD1P1Cy4X48%3D"
Content-Type: application/atom+xml
GData-Version: 1.4

HTTP/1.1 401 Unknown authorization header
WWW-Authenticate: GoogleLogin realm="http://www.google.com/accounts/ClientLogin", service="jotspot"
Content-Type: text/html; charset=UTF-8
Date: Thu, 17 May 2012 17:07:43 GMT
Expires: Thu, 17 May 2012 17:07:43 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Transfer-Encoding: chunked
4

1 回答 1

2

原来我在实现 OAuth 库时遇到了问题。这一切现在都按预期工作。下面是我的工作代码..

此示例要求您拥有适用于 PHP 的 Google API 客户端库:http ://code.google.com/p/google-api-php-client/

$CONSUMER_KEY = "yourdomain.com";
$CONSUMER_SECRET = "yoursecret";
$consumer = new apiClientOAuthConsumer($CONSUMER_KEY, $CONSUMER_SECRET, NULL);

$user = 'admin.user@yourdomain.com';
$base_feed = "https://sites.google.com/feeds/site/$CONSUMER_KEY/";
$params = array('max-results' => 50, 'xoauth_requestor_id' => $user, 'include-all-sites' => 'true');  
$request = apiClientOAuthRequest::from_consumer_and_token($consumer, NULL, "GET", $base_feed, $params);

$request->sign_request(new apiClientOAuthSignatureMethod_HMAC_SHA1(), $consumer, NULL);

$url = $base_feed . '?' . implode_assoc('=', '&', $params); 

$result = send_request($request->get_normalized_http_method(), $url, $request->to_header());

上面的代码使用下面的实用函数(来自: http: //gdatatips.blogspot.ca/search/label/oauth

/** 
 * Makes an HTTP request to the specified URL 
 * @param string $http_method The HTTP method (GET, POST, PUT, DELETE) 
 * @param string $url Full URL of the resource to access 
 * @param string $auth_header (optional) Authorization header 
 * @param string $postData (optional) POST/PUT request body 
 * @return string Response body from the server 
 */  
function send_request($http_method, $url, $auth_header=null, $postData=null) {  
  $curl = curl_init($url);  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  
  curl_setopt($curl, CURLOPT_FAILONERROR, false);  
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  

  switch($http_method) {  
    case 'GET':  
      if ($auth_header) {  
        curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));   
      }  
      break;  
    case 'POST':  
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml',   
                                                   $auth_header));   
      curl_setopt($curl, CURLOPT_POST, 1);                                         
      curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);  
      break;  
    case 'PUT':  
      curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/atom+xml',   
                                                   $auth_header));   
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);  
      curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);  
      break;  
    case 'DELETE':  
      curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header));   
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);   
      break;  
  }  
  $response = curl_exec($curl);  
  if (!$response) {  
    $response = curl_error($curl);  
  }  
  curl_close($curl);  
  return $response;  
}  

/** 
 * Joins key:value pairs by inner_glue and each pair together by outer_glue 
 * @param string $inner_glue The HTTP method (GET, POST, PUT, DELETE) 
 * @param string $outer_glue Full URL of the resource to access 
 * @param array $array Associative array of query parameters 
 * @return string Urlencoded string of query parameters 
 */  
function implode_assoc($inner_glue, $outer_glue, $array) {  
  $output = array();  
  foreach($array as $key => $item) {  
    $output[] = $key . $inner_glue . urlencode($item);  
  }  
  return implode($outer_glue, $output);  
}

我希望这可以帮助其他可能需要类似功能的人!

于 2012-05-18T20:45:52.260 回答