2011-01-21 137 views
0

我想验证我的Google AuthSub请求。基本上我需要生成一个私钥和相应的证书,将此证书上传到Google,然后在随后调用Google AuthSub时使用密钥进行签名。我认为最简单的方法是使用Java的keytool如下:Java中的安全API AuthSub(Google Calendar API)

# 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 

(如http://code.google.com/apis/gdata/docs/auth/authsub.html#keytool指定)

我上传的定证书,mycert.pem谷歌。在我的Java客户端中,我随后加载私钥如下:

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

加载此密钥时不会引发异常。然后在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); 

设置或交换令牌时不会抛出异常,而是尝试访问用户的资源时抛出异常。我不太确定该怎么做。唯一的例外如下:

Token invalid - Invalid AuthSub token. 

我把玩了一下周围有https://开头的http://为饲料URL和范围网址,但收效甚微,有可能我的天堂” t虽然尝试了一定的组合。

回答

0

看起来像所有上述工作正确,我只是有一个无关的编码错误。为了记录,http和https两个工作,只要一个使用一致(否则你会得到一个'范围'错误)。