1

我在使用 Java 客户端库(google-api-client 版本 1.13.2-beta、google-api-services-calendar 版本 v3-rev26-1.13.2-beta)对 Google Calendar API v3 进行身份验证时遇到问题。

我们有一个市场应用程序,安装该应用程序应为其提供读取(和写入)Google 日历事件所需的凭据。

我们使用带有 Hmac 的 2-legged OAuth 1.0,这意味着不需要令牌。这正是我想要做的,但使用日历而不是任务: https ://developers.google.com/google-apps/help/articles/2lo-in-tasks-for-marketplace

这是我从谷歌得到的错误:

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"
}

我们也有客户不使用 Marketplace 应用程序,但手动为我们的域授予了适当的权限,并且一切对他们来说都很好。只有使用 Marketplace 应用程序的客户存在此问题(使用不同的 consumerKey 和 consumerSecret),这让我相信代码没有任何问题,但我们忽略了 Google 设置中的某个地方。

Marketplace 应用程序的权限看起来不错。

我遵循了我能找到的所有指南,适合我们的设置(虽然没有很多),而且据我所知,我正在做正确的事情。

API 密钥似乎已正确设置(已授予对日历 API 的访问权限)。正如我所提到的,这适用于我们的客户不是来自 Marketplace 应用程序,并且他们使用相同的 API 密钥。

无论如何,这是一个失败的 JUnit 测试来重现错误:

import java.io.IOException;
import java.util.List;

import org.junit.Test;

import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarRequest;
import com.google.api.services.calendar.CalendarRequestInitializer;
import com.google.api.services.calendar.model.CalendarListEntry;

import junit.framework.Assert;

public class GoogleOAuthTest {

    @Test
    public void test() {
        final String API_KEY = "...";
        final String CONSUMER_KEY = "...";
        final String CONSUMER_KEY_SECRET = "...";
        final String GOOGLE_USER = "me@mydomain.com";

        // The 2-LO authorization section
        OAuthHmacSigner signer = new OAuthHmacSigner();
        signer.clientSharedSecret = CONSUMER_KEY_SECRET;

        final OAuthParameters oauthParameters = new OAuthParameters();
        oauthParameters.version = "1";
        oauthParameters.consumerKey = CONSUMER_KEY;
        oauthParameters.signer = signer;

        Calendar service = new Calendar.Builder(new NetHttpTransport(), new JacksonFactory(), oauthParameters)
                .setApplicationName("myapp")
                .setCalendarRequestInitializer(new CalendarRequestInitializer() {
                    @Override
                    protected void initializeCalendarRequest(CalendarRequest<?> request) throws IOException {
                        request.setKey(API_KEY);
                    }
                }).build();

        try {
            com.google.api.services.calendar.Calendar.CalendarList.List list = service.calendarList().list();
            list.getUnknownKeys().put("xoauth_requestor_id", GOOGLE_USER);
            // This is where I get the exception!
            List<CalendarListEntry> calendarList = list.execute().getItems();
        } catch (IOException e) {
            Assert.fail("Test failed: " + e.getMessage());
        }
    }
}

我可能做错了什么?我的代码是否有明显问题,或者我们在 Google 设置中可能遗漏了什么?

4

1 回答 1

1

不确定 API 密钥是什么,但除了请求初始化程序之外,我有非常相似的代码:

CalendarRequestInitializer initializer = new CalendarRequestInitializer() {

    @Override
    protected void initializeCalendarRequest(CalendarRequest<?> calendarRequest) throws IOException {
        ArrayMap<String, Object> customKeys = new ArrayMap<String, Object>();
        customKeys.add("xoauth_requestor_id", EMAIL_OF_THE_USER);
        calendarRequest.setUnknownKeys(customKeys);
    }
};

这对我有用......希望它有帮助;

更新使用您在 Marketplace -> My Vendor Profile -> My App 中单击“注册其他 API”链接时获得的 API 密钥。

于 2013-04-05T11:26:47.843 回答