public OAuthRequest get() { return new OAuthRequest(config, fetcher); } }
private void fetchRequestToken() throws OAuthRequestException, OAuthProtocolException { OAuthAccessor accessor = accessorInfo.getAccessor(); HttpRequest request = createRequestTokenRequest(accessor); List<Parameter> requestTokenParams = Lists.newArrayList(); addCallback(requestTokenParams); HttpRequest signed = sanitizeAndSign(request, requestTokenParams, true); OAuthMessage reply = sendOAuthMessage(signed); accessor.requestToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN); accessor.tokenSecret = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN_SECRET); }
/** * Strip out any owner or viewer identity information passed by the client. */ private List<Parameter> sanitize(List<Parameter> params) throws OAuthRequestException { ArrayList<Parameter> list = Lists.newArrayList(); for (Parameter p : params) { String name = p.getKey(); if (allowParam(name)) { list.add(p); } else { throw new OAuthRequestException(OAuthError.INVALID_PARAMETER, name); } } return list; }
/** * Does one of the following: * 1) Sends a request token request, and returns an approval URL to the calling app. * 2) Sends an access token request to swap a request token for an access token, and then asks * for data from the service provider. * 3) Asks for data from the service provider. */ private HttpResponseBuilder attemptFetch() throws OAuthRequestException, OAuthProtocolException { if (needApproval()) { // This is section 6.1 of the OAuth spec. checkCanApprove(); fetchRequestToken(); // This is section 6.2 of the OAuth spec. buildClientApprovalState(); buildAznUrl(); // break out of the content fetching chain, we need permission from // the user to do this return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_OK) .setStrictNoCache(); } else if (needAccessToken()) { // This is section 6.3 of the OAuth spec checkCanApprove(); exchangeRequestToken(); saveAccessToken(); buildClientAccessState(); } return fetchData(); }
String query = target.getQuery(); target.setQuery(null); params.addAll(sanitize(OAuth.decodeForm(query))); case URL_AND_FORM_PARAMS: try { params.addAll(sanitize(OAuth.decodeForm(base.getPostBodyAsString()))); } catch (IllegalArgumentException e) { addIdentityParams(authParams); addSignatureParams(authParams); overrideParameters(authParams); OAuthMessage signed = OAuthUtil.newRequestMessage(accessorInfo.getAccessor(), base.getMethod(), target.toString(), params); HttpRequest oauthHttpRequest = createHttpRequest(base, selectOAuthParams(signed));
/** * Fetch the response from the network using the right fetcher * Override this if you need to extend the current behavior of supported auth type. * * @param request * @return HttpResponse object fetched from network * @throws GadgetException */ protected HttpResponse fetchResponse(HttpRequest request) throws GadgetException { HttpResponse fetchedResponse; switch (request.getAuthType()) { case NONE: fetchedResponse = httpFetcher.fetch(request); break; case SIGNED: case OAUTH: fetchedResponse = oauthRequestProvider.get().fetch(request); break; case OAUTH2: fetchedResponse = oauth2RequestProvider.get().fetch(request); break; default: return HttpResponse.error(); } return fetchedResponse; }
/** * Fetch data, retrying in the event that that the service provider returns an error and we think * we can recover by restarting the protocol flow. */ private HttpResponseBuilder fetchWithRetry() throws OAuthRequestException { int attempts = 0; boolean retry; HttpResponseBuilder response = null; do { retry = false; ++attempts; try { response = attemptFetch(); } catch (OAuthProtocolException pe) { retry = handleProtocolException(pe, attempts); if (!retry) { if (pe.getProblemCode() != null) { throw new OAuthRequestException(pe.getProblemCode(), "Service provider rejected request", pe); } else { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Service provider rejected request", pe); } } } } while (retry); return response; }
/** * Do we need to exchange a request token for an access token? */ private boolean needAccessToken() { if (realRequest.getOAuthArguments().mustUseToken() && accessorInfo.getAccessor().requestToken != null && accessorInfo.getAccessor().accessToken == null) { return true; } return realRequest.getOAuthArguments().mayUseToken() && accessTokenExpired(); }
/** * Does one of the following: * 1) Sends a request token request, and returns an approval URL to the calling app. * 2) Sends an access token request to swap a request token for an access token, and then asks * for data from the service provider. * 3) Asks for data from the service provider. */ private HttpResponseBuilder attemptFetch() throws OAuthRequestException, OAuthProtocolException { if (needApproval()) { // This is section 6.1 of the OAuth spec. checkCanApprove(); fetchRequestToken(); // This is section 6.2 of the OAuth spec. buildClientApprovalState(); buildAznUrl(); // break out of the content fetching chain, we need permission from // the user to do this return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_OK) .setStrictNoCache(); } else if (needAccessToken()) { // This is section 6.3 of the OAuth spec checkCanApprove(); exchangeRequestToken(); saveAccessToken(); buildClientAccessState(); } return fetchData(); }
String query = target.getQuery(); target.setQuery(null); params.addAll(sanitize(OAuth.decodeForm(query))); case URL_AND_FORM_PARAMS: try { params.addAll(sanitize(OAuth.decodeForm(base.getPostBodyAsString()))); } catch (IllegalArgumentException e) { addIdentityParams(authParams); addSignatureParams(authParams); overrideParameters(authParams); OAuthMessage signed = OAuthUtil.newRequestMessage(accessorInfo.getAccessor(), base.getMethod(), target.toString(), params); HttpRequest oauthHttpRequest = createHttpRequest(base, selectOAuthParams(signed));
/** * Send an OAuth GET request to the given URL. */ public HttpResponse sendGet(String target) throws Exception { HttpRequest request = new HttpRequest(Uri.parse(target)); request.setOAuthArguments(recallState()); OAuthRequest dest = createRequest(); request.setIgnoreCache(ignoreCache); request.setSecurityToken(securityToken); HttpResponse response = dest.fetch(request); saveState(response); return response; }
/** * Fetch data, retrying in the event that that the service provider returns an error and we think * we can recover by restarting the protocol flow. */ private HttpResponseBuilder fetchWithRetry() throws OAuthRequestException { int attempts = 0; boolean retry; HttpResponseBuilder response = null; do { retry = false; ++attempts; try { response = attemptFetch(); } catch (OAuthProtocolException pe) { retry = handleProtocolException(pe, attempts); if (!retry) { if (pe.getProblemCode() != null) { throw new OAuthRequestException(pe.getProblemCode(), "Service provider rejected request", pe); } else { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Service provider rejected request", pe); } } } } while (retry); return response; }
/** * Do we need to exchange a request token for an access token? */ private boolean needAccessToken() { if (realRequest.getOAuthArguments().mustUseToken() && accessorInfo.getAccessor().requestToken != null && accessorInfo.getAccessor().accessToken == null) { return true; } return realRequest.getOAuthArguments().mayUseToken() && accessTokenExpired(); }
/** * Does one of the following: * 1) Sends a request token request, and returns an approval URL to the calling app. * 2) Sends an access token request to swap a request token for an access token, and then asks * for data from the service provider. * 3) Asks for data from the service provider. */ private HttpResponseBuilder attemptFetch() throws OAuthRequestException, OAuthProtocolException { if (needApproval()) { // This is section 6.1 of the OAuth spec. checkCanApprove(); fetchRequestToken(); // This is section 6.2 of the OAuth spec. buildClientApprovalState(); buildAznUrl(); // break out of the content fetching chain, we need permission from // the user to do this return new HttpResponseBuilder() .setHttpStatusCode(HttpResponse.SC_OK) .setStrictNoCache(); } else if (needAccessToken()) { // This is section 6.3 of the OAuth spec checkCanApprove(); exchangeRequestToken(); saveAccessToken(); buildClientAccessState(); } return fetchData(); }
String query = target.getQuery(); target.setQuery(null); params.addAll(sanitize(OAuth.decodeForm(query))); case URL_AND_FORM_PARAMS: try { params.addAll(sanitize(OAuth.decodeForm(base.getPostBodyAsString()))); } catch (IllegalArgumentException e) { addIdentityParams(authParams); addSignatureParams(authParams); overrideParameters(authParams); OAuthMessage signed = OAuthUtil.newRequestMessage(accessorInfo.getAccessor(), base.getMethod(), target.toString(), params); HttpRequest oauthHttpRequest = createHttpRequest(base, selectOAuthParams(signed));
/** * Send an OAuth GET request to the given URL. */ public HttpResponse sendGet(String target) throws Exception { HttpRequest request = new HttpRequest(Uri.parse(target)); request.setOAuthArguments(recallState()); OAuthRequest dest = createRequest(); request.setIgnoreCache(ignoreCache); request.setSecurityToken(securityToken); HttpResponse response = dest.fetch(request); saveState(response); return response; }
private void fetchRequestToken() throws OAuthRequestException, OAuthProtocolException { OAuthAccessor accessor = accessorInfo.getAccessor(); HttpRequest request = createRequestTokenRequest(accessor); List<Parameter> requestTokenParams = Lists.newArrayList(); addCallback(requestTokenParams); HttpRequest signed = sanitizeAndSign(request, requestTokenParams, true); OAuthMessage reply = sendOAuthMessage(signed); accessor.requestToken = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN); accessor.tokenSecret = OAuthUtil.getParameter(reply, OAuth.OAUTH_TOKEN_SECRET); }
public OAuthRequest get() { return new OAuthRequest(config, fetcher); } }
/** * Fetch data, retrying in the event that that the service provider returns an error and we think * we can recover by restarting the protocol flow. */ private HttpResponseBuilder fetchWithRetry() throws OAuthRequestException { int attempts = 0; boolean retry; HttpResponseBuilder response = null; do { retry = false; ++attempts; try { response = attemptFetch(); } catch (OAuthProtocolException pe) { retry = handleProtocolException(pe, attempts); if (!retry) { if (pe.getProblemCode() != null) { throw new OAuthRequestException(pe.getProblemCode(), "Service provider rejected request", pe); } else { throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Service provider rejected request", pe); } } } } while (retry); return response; }
/** * Do we need to exchange a request token for an access token? */ private boolean needAccessToken() { if (realRequest.getOAuthArguments().mustUseToken() && accessorInfo.getAccessor().requestToken != null && accessorInfo.getAccessor().accessToken == null) { return true; } return realRequest.getOAuthArguments().mayUseToken() && accessTokenExpired(); }