1

我想使用新的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;
    }
}

我的域配置如下所示:
高级工具 => 管理第三方 OAuth 客户端访问 在此处输入图像描述

我的控制台 API 如下所示:

在此处输入图像描述

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

但是在我传递任何其他域用户的情况下,我总是得到“无效的凭据”异常。堆栈跟踪:

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。我在哪里犯了错误?

4

1 回答 1

2

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

于 2013-02-14T09:12:52.567 回答