我正在嘗試使用fitbit4j爲fitbit創建應用程序。我發現他們的示例代碼 在 https://github.com/apakulov/fitbit4j/blob/master/fitbit4j-example-client/src/main/java/com/fitbit/web/FitbitApiAuthExampleServlet.java嘗試完成授權時無法識別的臨時令牌:FITBIT4J
當我試圖實現它時,我收到很多錯誤。下面 是他們的doGet()函數
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
FitbitAPIClientService<FitbitApiClientAgent> apiClientService = new FitbitAPIClientService<FitbitApiClientAgent>(
new FitbitApiClientAgent(apiBaseUrl, fitbitSiteBaseUrl, credentialsCache),
clientConsumerKey,
clientSecret,
credentialsCache,
entityCache,
subscriptionStore
);
if (request.getParameter("completeAuthorization") != null) {
String tempTokenReceived = request.getParameter(OAUTH_TOKEN);
String tempTokenVerifier = request.getParameter(OAUTH_VERIFIER);
APIResourceCredentials resourceCredentials = apiClientService.getResourceCredentialsByTempToken(tempTokenReceived);
if (resourceCredentials == null) {
throw new ServletException("Unrecognized temporary token when attempting to complete authorization: " + tempTokenReceived);
}
// Get token credentials only if necessary:
if (!resourceCredentials.isAuthorized()) {
// The verifier is required in the request to get token credentials:
resourceCredentials.setTempTokenVerifier(tempTokenVerifier);
try {
// Get token credentials for user:
apiClientService.getTokenCredentials(new LocalUserDetail(resourceCredentials.getLocalUserId()));
} catch (FitbitAPIException e) {
throw new ServletException("Unable to finish authorization with Fitbit.", e);
}
}
try {
UserInfo userInfo = apiClientService.getClient().getUserInfo(new LocalUserDetail(resourceCredentials.getLocalUserId()));
request.setAttribute("userInfo", userInfo);
request.getRequestDispatcher("/fitbitApiAuthExample.jsp").forward(request, response);
} catch (FitbitAPIException e) {
throw new ServletException("Exception during getting user info", e);
}
} else {
try {
response.sendRedirect(apiClientService.getResourceOwnerAuthorizationURL(new LocalUserDetail("-"), exampleBaseUrl + "/fitbitApiAuthExample?completeAuthorization="));
} catch (FitbitAPIException e) {
throw new ServletException("Exception during performing authorization", e);
}
}
}
當我運行它進入了 '其他' 部分首先代碼和我得到 的URL 本地主機:8080/fitbitApiAuthExample completeAuthorization = &的oauth_token = 5bccadXXXXXXXXXXXXXXXXXXXXXXXXXX & oauth_verifier = h35kXXXXXXXXXXXXXXXXX,我也得到了fitbit登錄屏幕,當我登錄 和自 'completeAuthorization == NULL',
我t再次執行else部分。所以我手動添加一個值,以便它將進入'if'部分。 因此,新的URL成爲 本地主機:8080/fitbitApiAuthExample completeAuthorization =成功&的oauth_token = 5bccadXXXXXXXXXXXXXXXXXXXXXXXXXX & oauth_verifier = h35kXXXXXXXXXXXXXXXXX,進入了 '如果' 部分。
現在是越來越異常 「試圖完成授權時無法識別的臨時令牌:」我試過很多解決方法,但仍然無法理解的錯誤。
請幫忙。