Skip to content

Instantly share code, notes, and snippets.

@shikajiro
Last active August 29, 2015 14:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save shikajiro/c7c9471ec2a25480b130 to your computer and use it in GitHub Desktop.
AndroidからOAuth2を使ってGoogle SpreadSheetへアクセスする方法 ref: http://qiita.com/shikajiro/items/63200af32b280be48bb5
//※ここにユーザーアカウント選択画面があると素敵だと思います。
//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