【问题标题】:How to instantiate a com.google.api.services.calendar.Calendar object given an oAuth token?如何在给定 oAuth 令牌的情况下实例化 com.google.api.services.calendar.Calendar 对象?
【发布时间】:2013-08-27 18:58:39
【问题描述】:

我已使用此代码为我的 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;
}

现在如何使用该 oAuth 令牌实例化 com.google.api.services.calendar.Calendar 对象,以便我可以代表该用户访问日历 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;
}

【问题讨论】:

    标签: android oauth google-calendar-api


    【解决方案1】:

    在查看了一些 Google 日历 API 和示例之后,似乎将令牌分配给日历的最佳方式是在初始化它时。这需要一些设置,看看这个链接:

    https://developers.google.com/google-apps/calendar/instantiate

    如果您已经经历过,我深表歉意,但看起来您可以对该示例进行一些调整。

    就在日历初始化之前,这段代码被执行:

        GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
        response.accessToken, httpTransport, jsonFactory, clientId, clientSecret,
        response.refreshToken);
    

    这又被用作日历初始化的一部分。

    查看 GoogleAccessProtectedResource 的文档,似乎存在一个只接受访问令牌的构造函数。

    http://javadoc.google-api-java-client.googlecode.com/hg/1.4.1-beta/com/google/api/client/googleapis/auth/oauth2/draft10/GoogleAccessProtectedResource.html#GoogleAccessProtectedResource(java.lang.String)

    您可以使用您之前在方法中请求过的令牌,然后使用上面 google 日历实例化链接中描述的其他几个对象,您应该能够使用给定的访问令牌正确实例化日历。

    希望这会有所帮助,

    编辑

    看起来 GoogleAccessProtectedResource 实际上将被弃用,或者已经被弃用。

    javadoc 声明:

    “已弃用。(计划在 1.8 中删除)使用 GoogleCredential”

    http://javadoc.google-api-java-client.googlecode.com/hg/1.7.0-beta/com/google/api/client/googleapis/auth/oauth2/draft10/GoogleAccessProtectedResource.html

    看来您需要用 GoogleCredential 来替换 GoogleAccessProtectedResource。我发现您可以像这样使用访问令牌设置凭据:

    GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
    

    然后您可以通过以下方式创建新日历:

    Calendar service = new Calendar.Builder(httpTransport, jsonFactory,
                credential).build();
    

    httpTransport 和 jsonFactory 与其他示例类似。

    祝你好运!

    【讨论】:

    • 这看起来正是我所需要的,但是我在哪里下载具有GoogleAccessProtectedResource 的jar?它似乎没有包含在 Google Calendar API java 客户端中。
    • 这个方法好像也被弃用了,确定可以用吗?
    • 这不是很有效,虽然我认为它很接近。我收到无效凭据的 401 错误,知道是什么原因造成的吗?
    • hmm 401 通常是未经授权的代码,这意味着令牌可能已过期。我会验证您是否使用了正确的 API 密钥,并且一切设置正确。我今天没有太多时间进一步调查这个问题,但这个链接可能会有所帮助。 developers.google.com/drive/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多