Google APIs Client Library for Java
のAndroid向けライブラリのソースを公開しました。
(URL は http://code.google.com/p/android-library-google-api-java/ です。)
単に,Google SpreadSheets に対して,OAuth 1.0a で
- 新しいSpreadSheetの作成
- worksheet entryの追加
- セルの更新
I upload sources of my android library to use Google APIs Client Library for Java.
This is a just sample for Google Spread Sheet, but it can create new spreadsheet and worksheet, rename the title of worksheet entry, and put cells on your worksheet with OAuth1.0a.
URL is http://code.google.com/p/android-library-google-api-java/.
I am preparing JavaDoc and classes for other services. But I am not good engineer, so speed to update migh be very slow.
Please contact me if you have any requests, suggestions, corrections and bug reports.
https://spreadsheets.google.com/feeds/spreadsheets/private/fullに送信すれば,認証されたユーザのspreadsheetsのfeedを入手することができます。
String token = "your access token"; String tokenSecret = "your access tokenSecret"; HttpTransport transport = new ApacheHttpTransport(); OAuthParameters parameters = createOAuthParameters(token, tokenSecret); parameters.signRequestsUsingAuthorizationHeader(transport); getSpreadSheets(transport);
public OAuthParameters createOAuthParameters(String token, String tokenSecret) { OAuthParameters authorizer = new OAuthParameters(); authorizer.consumerKey = "anonymous"; authorizer.signer = createOAuthSigner(tokenSecret); authorizer.token = token; return authorizer; }
public OAuthHmacSigner createOAuthSigner(String tokenSecret) { OAuthHmacSigner signer = new OAuthHmacSigner(); if (tokenSecret != null) { signer.tokenSharedSecret = tokenSecret; } signer.clientSharedSecret = "anonymous"; return signer; }
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(); } }
<?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>
Consider, for example, what the browser application does when the user follows a link on a web page. It first tries to display the data (as it could if the link was to an HTML page). If it can't display the data, it puts together an implicit intent with the scheme and data type and tries to start an activity that can do the job. If there are no takers, it asks the download manager to download the data. That puts it under the control of a content provider, so a potentially larger pool of activities (those with filters that just name a data type) can respond.とありますように,ブラウザでWebページ上のリンクをクリックして, データが表示されない場合,該当する暗黙のintentを発行するようになっています。 OAuth認証では,この機能を利用して,callback urlに自分のアプリを起動できるようなurlを指定します。
GoogleOAuthGetTemporaryToken temporaryToken = new GoogleOAuthGetTemporaryToken(); OAuthHmacsinger signer = new OAuthHmacSigner(); signer.clientSharedSecret = "anonymous"; temporaryToken.signer = signer; temporaryToken.displayName = appname; temporaryToken.consumerKey = "anonymous"; temporaryToken.scope = "https://spreadsheets.google.com/feeds/"; temporaryToken.transport = transport; temporaryToken.callback = "x-myspreadsheet://com.example.abekatsu/";
OAuthCredentialsResponse response = temporaryToken.execute(); GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl = new GoogleOAuthAuthorizeTemporaryTokenUrl(); authorizeUrl.template = "mobile"; authorizeUrl.set("scope", scope); authorizeUrl.set("domain", "anonymous"); authorizeUrl.set("xoauth_displayname", appname); authorizeUrl.temporaryToken = response.token;
Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(temporaryTokenUrl.build())); startActivity(intent);
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean isViewAction = Intent.ACTION_VIEW.equals(getIntent().getAction()); if (isViewAction) { Uri uri = this.getIntent().getData(); if (uri != null) { Log.d(TAG, "uri: " + uri.toString()); } } }
D/OkodukaiNoteActivity( 235): uri: x-okozukai:///?oauth_verifier=ここにoauth_verifierの文字列&oauth_token=ここにoauth_tokenの文字列
Androidアプリで扱うデータを外部のSpreadsheetに保存することを目的とします。
HttpTransport transport = new ApacheHttpTransport(); GoogleOAuthGetTemporaryToken temporaryToken = new GoogleOAuthGetTemporaryToken(); OAuthHmacsinger signer = new OAuthHmacSigner(); signer.clientSharedSecret = "anonymous"; temporaryToken.signer = signer; temporaryToken.displayName = appname; temporaryToken.consumerKey = "anonymous"; temporaryToken.scope = "https://spreadsheets.google.com/feeds/"; temporaryToken.transport = transport;
OAuthCredentialsResponse credentials = temporaryToken.execute();
if (credentials != null) { GoogleOAuthAuthorizeTemporaryTokenUrl temporaryTokenUrl = new GoogleOAuthAuthorizeTemporaryTokenUrl(); temporaryTokenUrl.template = "mobile"; temporaryTokenUrl.temporaryToken = credentials.token; // Uri uri = new Uri(temporaryTokenUrl.build()); Log.d(TAG, "temoraryTokenUrl: " + temporaryTokenUrl.build()); }
https://www.google.com/accounts/OAuthAuthorizeToken?btmpl=mobile&oauth_token=...some_oauth_token....