Thursday, April 07, 2011

Google Spreadsheets を Androidから扱う (4)

前回,access Token と access TokenSecretを取得しました。 これを用いて,自分のアカウントのspread sheet一覧を取得してみます。
Retrieving a list of spreadsheetsを読みますと,
https://spreadsheets.google.com/feeds/spreadsheets/private/full
に送信すれば,認証されたユーザのspreadsheetsのfeedを入手することができます。
具体的なコードは以下の通りです。

HttpTransporへの署名

String token = "your access token";
String tokenSecret = "your access tokenSecret";
HttpTransport transport = new ApacheHttpTransport();
OAuthParameters parameters = createOAuthParameters(token, tokenSecret);
parameters.signRequestsUsingAuthorizationHeader(transport);
getSpreadSheets(transport);
各メソッドの内容は以下の通りです。
createOAuthParameters
public OAuthParameters createOAuthParameters(String token, String tokenSecret) {
    OAuthParameters authorizer = new OAuthParameters();
    authorizer.consumerKey = "anonymous";
    authorizer.signer = createOAuthSigner(tokenSecret);
    authorizer.token = token;
    return authorizer;
}
createOAuthSigner
public OAuthHmacSigner createOAuthSigner(String tokenSecret) {
    OAuthHmacSigner signer = new OAuthHmacSigner();
    if (tokenSecret != null) {
        signer.tokenSharedSecret = tokenSecret;
    }
    signer.clientSharedSecret = "anonymous";
    return signer;
}
getSpreadSheet
public void getSpreadSheets(HttpTransport transport) {
    // Set AtomParser
    AtomParser parser = new AtomParser();
    XmlNamespaceDictionary dictionary = new XmlNamespaceDictionary();
    dictionary.set("", Atom.ATOM_NAMESPACE);
    dictionary.set("docs", "http://schemas.google.com/docs/2007");
    dictionary.set("batch", "http://schemas.google.com/gdata/batch");
    dictionary.set("gd", "http://schemas.google.com/g/2005");
    dictionary.set("gd:etag", "W/"DEMCQHk7eSt7ImA9WhZSGUw."");
    dictionary.set("openSearch", "http://a9.com/-/spec/opensearch/1.1/");
    dictionary.set("app", "http://www.w3.org/2007/app");
    parser.namespaceDictionary = dictionary;
    transport.addParser(parser);

    // Set Http Headers
    HttpHeaders headers = transport.defaultHeaders;
    headers.set("User-Agent", appname);
    headers.set("GData-Version", "3.0");

    HttpRequest request = transport.buildGetRequest();
    request.url = new GoogleUrl("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    try {
        HttpResponse response = request.execute();
        // null chekc していないので注意
        InputStreamReader inr = new InputStreamReader(response.getEntity().getContent()); 
        BufferedReader reader = new BufferedReader(inr, 256);
        while ((str = reader.readLine()) != null) {
            Log.d(TAG, line);
        }
        reader.close();
        inr.close();
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }
}
これを実行し,正常にHttpResponseが得られますと,以下のようなXML documentが得られます。
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/"DEMCQHk7eSt7ImA9WhZSGUw."'>
  <id>https://spreadsheets.google.com/feeds/spreadsheets/private/full</id>
  <updated>2011-04-04T11:27:41.701Z</updated>
  <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#spreadsheet'/>
  <title>Available Spreadsheets - Maskes(user's email address)</title>
  <link rel='alternate' type='text/html' href='http://docs.google.com'/>
  <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/spreadsheets/private/full'/>
  <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/spreadsheets/private/full'/>
  <openSearch:totalResults>11</openSearch:totalResults>
  <openSearch:startIndex>1</openSearch:startIndex>
  <entry gd:etag='"BBQbQxQLQit7ImBr"'>
    <id>https://spreadsheets.google.com/feeds/spreadsheets/tRsAk0wSkUi4wCBZxOJITuw</id>
    <updated>2011-04-04T04:52:59.849Z</updated>
    <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#spreadsheet'/>
    <title>Hack For Japan Projects</title>
    <content type='application/atom+xml;type=feed' src='https://spreadsheets.google.com/feeds/worksheets/tRsAk0wSkUi4wCBZxOJITuw/private/full'/>
    <link rel='http://schemas.google.com/spreadsheets/2006#tablesfeed' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/tRsAk0wSkUi4wCBZxOJITuw/tables'/>
    <link rel='alternate' type='text/html' href='https://spreadsheets.google.com/ccc?key=0Amb6cvTCzTQRdFJzQWswd1NrVWk0d0NCWnhPSklUdXc'/>
    <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/spreadsheets/private/full/tRsAk0wSkUi4wCBZxOJITuw'/>
    <author>
      <name>Masked</name>
      <email>Masked</email>
    </author>
  </entry>
</feed>
次回は,得られた XML Document の parse を行います。

No comments:

There was an error in this gadget