AndroidからOAuth2を使ってGoogle SpreadSheetへアクセスする方法 ref: http://qiita.com/shikajiro/items/63200af32b280be48bb5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//※ここにユーザーアカウント選択画面があると素敵だと思います。 | |
//oauth tokenの取得 | |
String token; | |
try { | |
token = GoogleAuthUtil.getToken(this, "shikajiro@gmail.com", "oauth2:https://spreadsheets.google.com/feeds"); | |
Log.i("TAG", token); | |
} catch (UserRecoverableAuthException e) { | |
//最初のアクセスの場合かならずここに来る。 | |
//ユーザーに承認を求める画面が表示される。 | |
startActivityForResult(e.getIntent(), USER_RECOVERABLE_AUTH); | |
return; | |
}catch (IOException | GoogleAuthException e) { | |
Log.e("TAG", "", e); | |
return; | |
} | |
//Spreadsheetへアクセスするサービス | |
SpreadsheetService service = new SpreadsheetService("applicationName"); | |
service.setProtocolVersion(SpreadsheetService.Versions.V3); | |
service.setAuthSubToken(token);//tokenを設定 | |
//プライベート権限のシート全てにアクセスして、とりあえずタイトル表示する。 | |
try { | |
URL url = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full"); | |
SpreadsheetFeed spreadsheetFeed = service.getFeed(url, SpreadsheetFeed.class); | |
Log.i("TAG", spreadsheetFeed.getTitle().getPlainText()); | |
for (SpreadsheetEntry entry : spreadsheetFeed.getEntries()) { | |
Log.i("TAG", entry.getTitle().getPlainText()); | |
} | |
} catch (IOException | ServiceException e) { | |
Log.e("TAG", "", e); | |
} | |
} | |
@OnActivityResult(USER_RECOVERABLE_AUTH) | |
void onResult(){ | |
callOauthToken(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment