Google Calendar API返回“无效凭据”(401)

时间:2013-04-05 11:15:42

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

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

我们有一个市场应用,安装该应用应该为其提供阅读(和撰写)Google日历活动所需的凭据。

我们正在使用带有Hmac的双腿OAuth 1.0,这意味着不需要令牌。 这正是我想要做的,但使用Calendar而不是Tasks: 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密钥(授予对Calendar 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设置中遗漏了什么?

1 个答案:

答案 0 :(得分: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);
    }
};

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

更新使用在市场中点击“注册其他API”链接时获得的API密钥 - &gt;我的供应商资料 - &gt;我的应用程序。

相关问题