我已经使用此代码为我的 android 应用程序中使用的每个日历获取了 oAuth 令牌
private HashMap<String, String> getAuthrizedCalendarsOnPhone() {
HashMap<String, String> authorized_calendars = new HashMap<String, String>();
AccountManager acctmgr = AccountManager.get(app_context);
Account[] accounts = acctmgr.getAccountsByType("com.google");
for (Account account : accounts) {
String auth_token_type = "oauth2:https://www.googleapis.com/auth/calendar";
AccountManagerFuture<Bundle> amf = acctmgr.getAuthToken(account, auth_token_type, null, this, null, null);
String authToken;
try {
Bundle authTokenBundle = amf.getResult();
authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
} catch(Exception e) {
authToken = "";
}
authorized_calendars.put(account.name, authToken);
}
return authorized_calendars;
}
现在如何com.google.api.services.calendar.Calendar
使用该 oAuth 令牌实例化一个对象,以便我可以代表该用户访问日历 api?
即我可以做这样的事情
private HashMap<String, HCEvent> getCalendarEvents(String calendar_name) {
HashMap<String, HCEvent> return_map = new HashMap<String, HCEvent>();
com.google.api.services.calendar.Calendar service = null; //create a Calendar object using the oauth token for associated with calendar_name
com.google.api.services.calendar.model.Events events = service.events().list(calendar_name).setPageToken(pageToken).execute();
/*
* do something with the events
*/
return return_map;
}