@Override protected Response fetchResponse(HttpURLConnection httpUrlConnection) throws IOException { try { if (httpUrlConnection.getRequestMethod().equals(HttpMethod.GET.name())) { if (httpUrlConnection.getResponseCode() == HTTP_NOT_MODIFIED && currentETagRespThreadLocal.get() != null) { ETagResponse etagResp = currentETagRespThreadLocal.get(); return new Response(httpUrlConnection.getResponseCode(), etagResp.getBody()); } else { Response resp = super.fetchResponse(httpUrlConnection); if (httpUrlConnection.getHeaderField("ETag") != null) { etagCache.put(httpUrlConnection.getURL().toString(), new ETagResponse(httpUrlConnection.getHeaderField("ETag"), resp.getBody())); } return resp; } } else { return super.fetchResponse(httpUrlConnection); } } finally { currentETagRespThreadLocal.remove(); } }
throw new FacebookNetworkException("Facebook POST failed", response.getStatusCode()); String json = response.getBody();
protected String makeRequestAndProcessResponse(Requestor requestor) { Response response; // Perform a GET or POST to the API endpoint try { response = requestor.makeRequest(); } catch (Exception t) { throw new FacebookNetworkException(t); } // If we get any HTTP response code other than a 200 OK or 400 Bad Request // or 401 Not Authorized or 403 Forbidden or 404 Not Found or 500 Internal // Server Error or 302 Not Modified // throw an exception. if (HTTP_OK != response.getStatusCode() && HTTP_BAD_REQUEST != response.getStatusCode() && HTTP_UNAUTHORIZED != response.getStatusCode() && HTTP_NOT_FOUND != response.getStatusCode() && HTTP_INTERNAL_ERROR != response.getStatusCode() && HTTP_FORBIDDEN != response.getStatusCode() && HTTP_NOT_MODIFIED != response.getStatusCode()) { throw new FacebookNetworkException(response.getStatusCode()); } String json = response.getBody(); // If the response contained an error code, throw an exception. getFacebookExceptionGenerator().throwFacebookResponseStatusExceptionIfNecessary(json, response.getStatusCode()); // If there was no response error information and this was a 500 or 401 // error, something weird happened on Facebook's end. Bail. if (HTTP_INTERNAL_ERROR == response.getStatusCode() || HTTP_UNAUTHORIZED == response.getStatusCode()) { throw new FacebookNetworkException(response.getStatusCode()); } return json; }
protected String makeRequestAndProcessResponse(Requestor requestor) { Response response = null; // Perform a GET or POST to the API endpoint try { response = requestor.makeRequest(); } catch (Throwable t) { throw new FacebookNetworkException("Facebook request failed", t); } if (logger.isLoggable(INFO)) logger.info("Facebook responded with " + response); // If we get any HTTP response code other than a 200 OK or 400 Bad Request // or 401 Not Authorized or 403 Forbidden or 500 Internal Server Error, // throw an exception. if (HTTP_OK != response.getStatusCode() && HTTP_BAD_REQUEST != response.getStatusCode() && HTTP_UNAUTHORIZED != response.getStatusCode() && HTTP_INTERNAL_ERROR != response.getStatusCode() && HTTP_FORBIDDEN != response.getStatusCode()) throw new FacebookNetworkException("Facebook request failed", response.getStatusCode()); String json = response.getBody(); // If the response contained an error code, throw an exception. throwFacebookResponseStatusExceptionIfNecessary(json); // If there was no response error information and this was a 500 or 401 // error, something weird happened on Facebook's end. Bail. if (HTTP_INTERNAL_ERROR == response.getStatusCode() || HTTP_UNAUTHORIZED == response.getStatusCode()) throw new FacebookNetworkException("Facebook request failed", response.getStatusCode()); return json; }
private FacebookClient.AccessToken getFacebookUserToken(String code, String redirectUrl) throws IOException { String appId = "YOUR_APP_ID"; String secretKey = "YOUR_SECRET_KEY"; WebRequestor wr = new DefaultWebRequestor(); WebRequestor.Response accessTokenResponse = wr.executeGet( "https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl + "&client_secret=" + secretKey + "&code=" + code); return DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody()); }