列出不同用户的Google日历活动

时间:2013-02-13 11:51:43

标签: java google-api google-calendar-api google-api-java-client

我想使用新的Calendar API v3列出日历活动。我已经阅读了那篇文章(Calendar API v3, 2-legged OAuth & Java)并且我以相同的方式进行了身份验证。请看我的工厂:

import java.io.IOException;

import com.gene.herder.google.GoogleApiSettings;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Builder;
import com.google.api.services.calendar.CalendarRequest;
import com.google.api.services.calendar.CalendarRequestInitializer;

public final class CalendarApiFactory {

    private GoogleApiSettings settings;

    public CalendarApi createCalendarApi() {
        Calendar calendar = createBuilder(settings).build();

        CalendarApi calendarApi = new CalendarApi();
        calendarApi.setCalendar(calendar);
        return calendarApi;
    }

    private Builder createBuilder(GoogleApiSettings settings) {
        HttpTransport transport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();

        OAuthParameters oauthParameters = createOAuthParameters(settings);

        Builder builder = new Builder(transport, jsonFactory, oauthParameters);
        builder.setApplicationName("MY-COMPANY-MY-APP/1.0");
        builder.setCalendarRequestInitializer(new SettingsCalendarRequestInitializer(settings));
        return builder;
    }

    private OAuthParameters createOAuthParameters(GoogleApiSettings settings) {
        OAuthParameters oauthParameters = new OAuthParameters();
        oauthParameters.version = "1";
        oauthParameters.consumerKey = settings.getClientKey();
        oauthParameters.signer = createSigner(settings);
        return oauthParameters;
    }

    private OAuthHmacSigner createSigner(GoogleApiSettings settings) {
        OAuthHmacSigner signer = new OAuthHmacSigner();
        signer.clientSharedSecret = settings.getClientSecret();
        return signer;
    }

    public void setSettings(GoogleApiSettings settings) {
        this.settings = settings;
    }
}

class SettingsCalendarRequestInitializer extends CalendarRequestInitializer {

    private GoogleApiSettings settings;

    public SettingsCalendarRequestInitializer(GoogleApiSettings settings) {
        this.settings = settings;
    }

    @Override
    protected void initializeCalendarRequest(CalendarRequest<?> request) throws IOException {
        request.setKey(settings.getApiKey());
    }
}

之后我以这种方式实施了CalendarApi

import java.io.IOException;

import com.google.api.client.util.ArrayMap;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.Calendar.Events.Insert;
import com.google.api.services.calendar.Calendar.Events.List;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;

public class CalendarApi {

    private Calendar calendar;

    public Events listEvents(String calendarId) throws IOException {
        // Add the xoauth_requestor_id query parameter to let the API know
        // on behalf of which user the request is being made.
        ArrayMap<String, Object> customKeys = new ArrayMap<String, Object>();
        customKeys.add("xoauth_requestor_id", calendarId);

        List events = calendar.events().list("primary");
        events.setUnknownKeys(customKeys);
        return events.execute();
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }
}

我的域名配置如下:
高级工具=&gt;管理第三方OAuth客户端访问 enter image description here

我的Console API如下所示:

enter image description here

现在,如果我使用“user1@my-company-domain.com”参数调用我的listEvents方法,一切正常。我可以看到指定日历中的所有事件。

但在我通过任何其他域用户的情况下,我始终获得“无效凭据”异常。 Stacktrace:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
  "code" : 401,
  "errors" : [ {
    "domain" : "global",
    "location" : "Authorization",
    "locationType" : "header",
    "message" : "Invalid Credentials",
    "reason" : "authError"
  } ],
  "message" : "Invalid Credentials"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:143) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:115) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:309) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1090) ~[google-http-client-1.13.1-beta.jar:1.13.1-beta]
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:407) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:340) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:458) ~[google-api-client-1.13.2-beta.jar:1.13.2-beta]
    at com.gene.herder.google.calendar.CalendarApi.listEvents(CalendarApi.java:31) ~[CalendarApi.class:na]

我的maven依赖项如下所示:

<!-- Google API -->
<dependency>
  <groupId>com.google.apis</groupId>
  <artifactId>google-api-services-calendar</artifactId>
  <version>v3-rev22-1.13.2-beta</version>
</dependency>
<dependency>
  <groupId>com.google.http-client</groupId>
  <artifactId>google-http-client-jackson2</artifactId>
  <version>1.13.1-beta</version>
</dependency>
<dependency>
  <groupId>com.google.oauth-client</groupId>
  <artifactId>google-oauth-client-jetty</artifactId>
  <version>1.13.1-beta</version>
</dependency>

为什么它不起作用?我想我可以为任何我想要的用户使用该API。哪里弄错了?

1 个答案:

答案 0 :(得分:2)

我们必须在CPanel中启用两条腿OAuth访问控制选项。使用此选项API工作正常 您可以在以下位置找到此选项:
高级工具=&gt;管理OAuth域密钥=&gt;双腿OAuth访问控制