@Override public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) { Preconditions.checkArgument( Strings.isNullOrEmpty(extra), "Extra data not expected for OAuth flow"); Preconditions.checkArgument(initialAuthData == null, "Initial auth data not expected for " + config.getServiceName()); Map<String, String> params = new LinkedHashMap<>(); params.put("client_id", clientId); params.put("client_secret", clientSecret); params.put("grant_type", "authorization_code"); params.put("redirect_uri", callbackBaseUrl); params.put("code", authCode); HttpContent content = new UrlEncodedContent(params); try { OAuth2TokenResponse tokenResponse = OAuthUtils .makePostRequest(httpTransport, config.getTokenUrl(), content, OAuth2TokenResponse.class); return new TokensAndUrlAuthData( tokenResponse.getAccessToken(), tokenResponse.getRefreshToken(), config.getTokenUrl() ); } catch (IOException e) { throw new RuntimeException("Error getting token", e); // TODO } }
/** Posts a new status for the user, initially marked as private.**/ public void postStatus(String content, String idempotencyKey) throws IOException { ImmutableMap<String, String> formParams = ImmutableMap.of( "status", content, // Default everything to private to avoid a privacy incident "visibility", "private" ); UrlEncodedContent urlEncodedContent = new UrlEncodedContent(formParams); HttpRequest postRequest = TRANSPORT.createRequestFactory() .buildPostRequest( new GenericUrl(baseUrl + POST_URL), urlEncodedContent) .setThrowExceptionOnExecuteError(false); HttpHeaders headers = new HttpHeaders(); headers.setAuthorization("Bearer " + accessToken); if (!Strings.isNullOrEmpty(idempotencyKey)) { // This prevents the same post from being posted twice in the case of network errors headers.set("Idempotency-Key", idempotencyKey); } postRequest.setHeaders(headers); HttpResponse response = postRequest.execute(); validateResponse(postRequest, response, 200); }
private static String getGoogleIdToken(String jwt) throws Exception { final GenericData tokenRequest = new GenericData().set("grant_type", JWT_BEARER_TOKEN_GRANT_TYPE).set("assertion", jwt); final UrlEncodedContent content = new UrlEncodedContent(tokenRequest); final HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); final HttpRequest request = requestFactory .buildPostRequest(new GenericUrl(OAUTH_TOKEN_URI), content) .setParser(new JsonObjectParser(JacksonFactory.getDefaultInstance())); HttpResponse response; String idToken = null; response = request.execute(); GenericData responseData = response.parseAs(GenericData.class); idToken = (String) responseData.get("id_token"); return idToken; }
@Override public HttpContent getHttpContent() { Map<String, String> data = Maps.newHashMap(); data.put(REPORT_XML_KEY, reportDefinitionXml); return new UrlEncodedContent(data); } }
@Override public HttpContent getHttpContent() { Map<String, String> data = Maps.newHashMap(); data.put(REPORT_XML_KEY, reportDefinitionXml); return new UrlEncodedContent(data); } }
@Override public HttpContent getHttpContent() { Map<String, String> data = Maps.newHashMap(); data.put(REPORT_QUERY_KEY, reportQuery); data.put(FORMAT_KEY, format); return new UrlEncodedContent(data); } }
@Override public HttpContent getHttpContent() { Map<String, String> data = Maps.newHashMap(); data.put(REPORT_QUERY_KEY, reportQuery); data.put(FORMAT_KEY, format); return new UrlEncodedContent(data); } }
public void execute(String webHookUrl, Map<String, Object> payload) throws IOException { String jsonEncodedMessage = new Gson().toJson(payload); HashMap<Object, Object> payloadToSend = Maps.newHashMap(); payloadToSend.put("payload", jsonEncodedMessage); requestFactory.buildPostRequest(new GenericUrl(webHookUrl), new UrlEncodedContent(payloadToSend)) .execute(); } }
public void execute(String webHookUrl, Map<String, Object> payload) throws IOException { String jsonEncodedMessage = new Gson().toJson(payload); HashMap<Object, Object> payloadToSend = Maps.newHashMap(); payloadToSend.put("payload", jsonEncodedMessage); requestFactory.buildPostRequest(new GenericUrl(webHookUrl), new UrlEncodedContent(payloadToSend)) .execute(); } }
public String postEncodedEntry (String url, Map<String, String> params, boolean needAccessToken) throws DoubanException, IOException { UrlEncodedContent content = new UrlEncodedContent(params); HttpRequest method = requestFactory.buildPostRequest(new GenericUrl(url), content); return httpRequest(method, needAccessToken).parseAsString(); }
data.put("username", username); data.put("password", password); HttpContent content = new UrlEncodedContent(data);
public String postMultipartEntry (String url, Map<String, String> params, boolean needAccessToken) throws DoubanException, IOException { UrlEncodedContent uec = new UrlEncodedContent(params); MultipartRelatedContent content = new MultipartRelatedContent(uec); HttpRequest method = requestFactory.buildPostRequest(new GenericUrl(url), content); return httpRequest(method, needAccessToken).parseAsString(); }
/** * Clears all data in the Datastore. * * @throws LocalDevelopmentDatastoreException */ public void clear() throws LocalDevelopmentDatastoreException { HttpRequestFactory client = remoteRpc.getHttpRequestFactory(); try { Map<String, String> params = new HashMap<String, String>(); params.put("action", "Clear Datastore"); UrlEncodedContent content = new UrlEncodedContent(params); GenericUrl url = new GenericUrl(host + "/_ah/admin/datastore"); HttpResponse httpResponse = client.buildPostRequest(url, content).execute(); if (!httpResponse.isSuccessStatusCode()) { throw new LocalDevelopmentDatastoreException( "Clear Datastore returned http status " + httpResponse.getStatusCode()); } } catch (IOException e) { throw new LocalDevelopmentDatastoreException( "Exception trying to clear the dev datastore", e); } }
/** * Clears all data in the Datastore. * * @throws LocalDevelopmentDatastoreException */ public void clear() throws LocalDevelopmentDatastoreException { HttpRequestFactory client = remoteRpc.getHttpRequestFactory(); try { Map<String, String> params = new HashMap<String, String>(); params.put("action", "Clear Datastore"); UrlEncodedContent content = new UrlEncodedContent(params); GenericUrl url = new GenericUrl(host + "/_ah/admin/datastore"); HttpResponse httpResponse = client.buildPostRequest(url, content).execute(); if (!httpResponse.isSuccessStatusCode()) { throw new LocalDevelopmentDatastoreException( "Clear Datastore returned http status " + httpResponse.getStatusCode()); } } catch (IOException e) { throw new LocalDevelopmentDatastoreException( "Exception trying to clear the dev datastore", e); } }
void stopDatastoreInternal() throws LocalDevelopmentDatastoreException { // No need to kill the process we started, this function will take care of it. HttpRequestFactory client = remoteRpc.getHttpRequestFactory(); Map<String, String> params = new HashMap<String, String>(); UrlEncodedContent content = new UrlEncodedContent(params); GenericUrl url = new GenericUrl(host + "/_ah/admin/quit"); try { HttpResponse httpResponse = client.buildPostRequest(url, content).execute(); if (!httpResponse.isSuccessStatusCode()) { throw new LocalDevelopmentDatastoreException( "Request to shutdown local datastore returned http error code " + httpResponse.getStatusCode()); } } catch (IOException e) { throw new LocalDevelopmentDatastoreException( "Exception trying to stop the dev datastore", e); } }
void stopDatastoreInternal() throws LocalDevelopmentDatastoreException { // No need to kill the process we started, this function will take care of it. HttpRequestFactory client = remoteRpc.getHttpRequestFactory(); Map<String, String> params = new HashMap<String, String>(); UrlEncodedContent content = new UrlEncodedContent(params); GenericUrl url = new GenericUrl(host + "/_ah/admin/quit"); try { HttpResponse httpResponse = client.buildPostRequest(url, content).execute(); if (!httpResponse.isSuccessStatusCode()) { throw new LocalDevelopmentDatastoreException( "Request to shutdown local datastore returned http error code " + httpResponse.getStatusCode()); } } catch (IOException e) { throw new LocalDevelopmentDatastoreException( "Exception trying to stop the dev datastore", e); } }
/** * Authenticates based on the provided field values. * * @throws HttpResponseException if the authentication response has an error code, such as for a * CAPTCHA challenge. Call {@link HttpResponseException#response exception.response}. * {@link HttpResponse#parseAs(Class) parseAs}({@link ClientLogin.ErrorInfo * ClientLoginAuthenticator.ErrorInfo}.class) to parse the response. * @throws IOException some other kind of I/O exception */ public Response authenticate() throws HttpResponseException, IOException { GenericUrl url = serverUrl.clone(); url.appendRawPath("/accounts/ClientLogin"); UrlEncodedContent content = new UrlEncodedContent(); content.data = this; HttpRequest request = transport.createRequestFactory().buildPostRequest(url, content); request.addParser(AuthKeyValueParser.INSTANCE); request.disableContentLogging = true; return request.execute().parseAs(Response.class); } }
public void intercept(HttpRequest request) throws IOException { if (overrideThisMethod(request)) { String requestMethod = request.getRequestMethod(); request.setRequestMethod(HttpMethods.POST); request.getHeaders().set(HEADER, requestMethod); if (requestMethod.equals(HttpMethods.GET)) { // take the URI query part and put it into the HTTP body request.setContent(new UrlEncodedContent(request.getUrl().clone())); // remove query parameters from URI request.getUrl().clear(); } else if (request.getContent() == null) { // Google servers will fail to process a POST unless the Content-Length header is specified request.setContent(new EmptyContent()); } } }
/** * Refreshes the OAuth2 access token by getting a new access token from the refresh token */ @Override public AccessToken refreshAccessToken() throws IOException { if (refreshToken == null) { throw new IllegalStateException("UserCredentials instance cannot refresh because there is no" + " refresh token."); } GenericData tokenRequest = new GenericData(); tokenRequest.set("client_id", clientId); tokenRequest.set("client_secret", clientSecret); tokenRequest.set("refresh_token", refreshToken); tokenRequest.set("grant_type", GRANT_TYPE); UrlEncodedContent content = new UrlEncodedContent(tokenRequest); HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content); request.setParser(new JsonObjectParser(JSON_FACTORY)); HttpResponse response = request.execute(); GenericData responseData = response.parseAs(GenericData.class); String accessToken = OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX); int expiresInSeconds = OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX); long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000; return new AccessToken(accessToken, new Date(expiresAtMilliseconds)); }
/** * Refreshes the OAuth2 access token by getting a new access token from the refresh token */ @Override public AccessToken refreshAccessToken() throws IOException { if (refreshToken == null) { throw new IllegalStateException("UserCredentials instance cannot refresh because there is no" + " refresh token."); } GenericData tokenRequest = new GenericData(); tokenRequest.set("client_id", clientId); tokenRequest.set("client_secret", clientSecret); tokenRequest.set("refresh_token", refreshToken); tokenRequest.set("grant_type", GRANT_TYPE); UrlEncodedContent content = new UrlEncodedContent(tokenRequest); HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory(); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content); request.setParser(new JsonObjectParser(JSON_FACTORY)); HttpResponse response = request.execute(); GenericData responseData = response.parseAs(GenericData.class); String accessToken = OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX); int expiresInSeconds = OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX); long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000; return new AccessToken(accessToken, new Date(expiresAtMilliseconds)); }