简体   繁体   English

Java中的安全API AuthSub(Google Calendar API)

[英]Secure API AuthSub in Java (Google Calendar API)

I would like to authenticate my Google AuthSub requests. 我想验证我的Google AuthSub请求。 Basically I need to generate a private key and respective certificate, upload this certificate to Google, and sign with the key on subsequent calls to Google AuthSub. 基本上,我需要生成一个私钥和相应的证书,将此证书上传到Google,并在随后对Google AuthSub的调用中使用该密钥签名。 I think the most straightforward approach is to use Java's keytool as follows: 我认为最直接的方法是按如下方式使用Java的keytool:

# Generate the RSA keys and certificate
keytool -genkey -v -alias Example -keystore ./Example.jks\
  -keyalg RSA -sigalg SHA1withRSA\
  -dname "CN=www.example.com, OU=Engineering, O=My_Company, L=Mountain  View, ST=CA, C=US"\
  -storepass changeme -keypass changeme

# Output the public certificate to a file
keytool -export -rfc -keystore ./Example.jks -storepass changeme \
  -alias Example -file mycert.pem

(As specified by http://code.google.com/apis/gdata/docs/auth/authsub.html#keytool ) (由http://code.google.com/apis/gdata/docs/auth/authsub.html#keytool指定)

I uploaded the given certificate, mycert.pem to Google. 我将给定的证书mycert.pem上传到Google。 In my Java client, I then loaded the private key as follows: 然后,在Java客户端中,按如下方式加载私钥:

PrivateKey key = AuthSubUtil.getPrivateKeyFromKeystore(
                               "Example.jks", "changeme", "Example", "changeme");

No exceptions are thrown upon loading this key. 加载此密钥时不会引发任何异常。 The key is then used during AuthSub calls, as follows. 然后在AuthSub调用期间使用该密钥,如下所示。

String requestUrl =
  AuthSubUtil.getRequestUrl("http://www.example.com/RetrieveToken",
                            "https://www.google.com/calendar/feeds/",
                            true,
                            true);
...
// Servlet context, user follows the 'next link' with token attached.
String onetimeUseToken = AuthSubUtil.getTokenFromReply(
                                           httpServletRequest.getQueryString());

// Exchange for the AuthSub token.
String sessionToken = AuthSubUtil.exchangeForSessionToken(onetimeUseToken, key);

// Use the token.
CalendarService.setAuthSubToken(sessionToken, key);

// Get calendars from the user.
URL feedUrl = 
    new URL("https://www.google.com/calendar/feeds/default/owncalendars/full");

// Exception is thrown HERE.
CalendarFeed resultFeed = service.getFeed(feedUrl, CalendarFeed.class);

The exception is not thrown while setting or exchanging the token, but rather upon attempting to access the user's resources. 在设置或交换令牌时不会引发异常,而是在尝试访问用户资源时引发异常。 I'm not quite sure what to make of this. 我不太确定该怎么做。 The exception is as follows: 异常如下:

Token invalid - Invalid AuthSub token.

I toyed around a bit with https:// versus http:// for the feed URL and scope URL, but with little success, it's possible I haven't tried a certain combination though. 对于Feed URL和范围URL,我用https://http://进行了比较,但是收效甚微,但是我可能没有尝试某种组合。

Seems like all of the above works correctly, I just had an irrelevant coding error. 似乎上述所有方法均能正常工作,但我只是遇到了无关的编码错误。 For the record, http and https both work as long as one is used consistently (otherwise you get a 'scope' error). 作为记录,http和https都可以使用,只要它们被一致使用即可(否则会出现“作用域”错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM