簡體   English   中英

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

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

我想驗證我的Google AuthSub請求。 基本上,我需要生成一個私鑰和相應的證書,將此證書上傳到Google,並在隨后對Google AuthSub的調用中使用該密鑰簽名。 我認為最直接的方法是按如下方式使用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

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

我將給定的證書mycert.pem上傳到Google。 然后,在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.

對於Feed URL和范圍URL,我用https://http://進行了比較,但是收效甚微,但是我可能沒有嘗試某種組合。

似乎上述所有方法均能正常工作,但我只是遇到了無關的編碼錯誤。 作為記錄,http和https都可以使用,只要它們被一致使用即可(否則會出現“作用域”錯誤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM