1

Google Sites API を使用して、現在認証されているユーザーが読み取り/書き込み/所有者のアクセス権を持つサイトだけでなく、ドメインのすべてのサイトのリストを取得しようとしています。私が知る限り、現在の API でこれを処理する機能はありません。

ただし、現在の API リファレンス (https://developers.google.com/google-apps/sites/docs/1.0/reference/) によると、パラメーターを使用して、ユーザーが少なくとも「表示」できるすべてのサイトを含めることができます。 . これをドメイン管理者ユーザーの使用と組み合わせると、私の要件を処理できるようです。

残念ながら、これを機能させることはできません。サイト フィードを使用していることを除いて、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 に答える