我在使用Java客户端库(Google-api-client版本1.13.2-beta,google-api-services-calendar版本)对Google Calendar API v3进行身份验证时遇到问题V3-rev26-1.13.2-β)。Google Calendar API返回“无效凭证”(401)
我们有一个市场应用程序,安装该应用程序应该为其提供读取(和编写)Google日历事件所需的凭据。
我们使用双腿OAuth 1.0和Hmac,这意味着无需令牌。 这正是我试图做的,但随着日历而不是任务: 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 = "[email protected]";
// 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设置中错过了什么?
我后来设置xoauth_requestor_id在我的代码,像这样: 'list.getUnknownKeys()把( “xoauth_requestor_id”,GOOGLE_USER);' 如果我跳过API密钥,我得到这个错误,而不是: '403禁止 { “代码”:403, “错误”:[{ “域”: “usageLimits”, “消息”: “访问未配置”, “原因”: “accessNotConfigured” } ], “message”:“Access Not Configured” – popstr 2013-04-05 11:35:54
我明白了,但是,嘿,这对我很有用。你拥有的其他代码是相同的,那么为什么不尝试? – koma 2013-04-05 11:37:49
我没有在任何地方使用API密钥,但是您是否设置了与您的市场列表相关的google API控制台:https://code.google.com/apis/console/? – koma 2013-04-05 11:41:57