2011-01-21 61 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兩個工作,只要一個使用一致(否則你會得到一個'範圍'錯誤)。