private GenericUrl buildTargetUrl(String path) { GenericUrl genericUrl = new GenericUrl(translate.getBaseUrl() + "v2/" + path); if (options.getApiKey() != null) { genericUrl.put("key", options.getApiKey()); } return genericUrl; }
/** * Return an URL that performs the authorization consent prompt web UI. * * @param userId Application's identifier for the end user. * @param state State that is passed on to the OAuth2 callback URI after the consent. * @param baseUri The URI to resolve the OAuth2 callback URI relative to. * @return The URL that can be navigated or redirected to. */ public URL getAuthorizationUrl(String userId, String state, URI baseUri) { URI resolvedCallbackUri = getCallbackUri(baseUri); String scopesString = Joiner.on(' ').join(scopes); GenericUrl url = new GenericUrl(userAuthUri); url.put("response_type", "code"); url.put("client_id", clientId.getClientId()); url.put("redirect_uri", resolvedCallbackUri); url.put("scope", scopesString); if (state != null) { url.put("state", state); } url.put("access_type", "offline"); url.put("approval_prompt", "force"); if (userId != null) { url.put("login_hint", userId); } url.put("include_granted_scopes", true); return url.toURL(); }
/** * Return an URL that performs the authorization consent prompt web UI. * * @param userId Application's identifier for the end user. * @param state State that is passed on to the OAuth2 callback URI after the consent. * @param baseUri The URI to resolve the OAuth2 callback URI relative to. * @return The URL that can be navigated or redirected to. */ public URL getAuthorizationUrl(String userId, String state, URI baseUri) { URI resolvedCallbackUri = getCallbackUri(baseUri); String scopesString = Joiner.on(' ').join(scopes); GenericUrl url = new GenericUrl(userAuthUri); url.put("response_type", "code"); url.put("client_id", clientId.getClientId()); url.put("redirect_uri", resolvedCallbackUri); url.put("scope", scopesString); if (state != null) { url.put("state", state); } url.put("access_type", "offline"); url.put("approval_prompt", "force"); if (userId != null) { url.put("login_hint", userId); } url.put("include_granted_scopes", true); return url.toURL(); }
String url = LICENSE_URL_BASE + "licenseNotification/" + appCode + "?alt=json&max-results=200"; GenericUrl url2 = new GenericUrl(new URL(url)); if (pageToken != null) { url2.put("start-token", pageToken); } else // can't have both params if (timestamp != null) { url2.put("timestamp", timestamp); } HttpRequest req = HTTP_TRANSPORT.createRequestFactory(googleCredential) .buildGetRequest(url2); req.setParser(JSON_FACTORY.createJsonObjectParser()); HttpResponse response = req.execute(); return response.parseAs(LicenseNotificationList.class);
HttpTransport httpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = httpTransport.createRequestFactory(); JSONParser parser = new JSONParser(); String query = "[{\"limit\": 5,\"name\":null,\"type\":\"/medicine/disease\"}]"; GenericUrl url = new GenericUrl("https://www.googleapis.com/freebase/v1/mqlread"); url.put("key", "YOUR-API-KEY-GOES-HERE"); url.put("query", query); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); JSONObject response = (JSONObject)parser.parse(httpResponse.parseAsString()); JSONArray results = (JSONArray)response.get("result"); for (Object result : results) { System.out.println(result.get("name").toString()); }
public JSONObject getCursorAndResponse(String mqlQuery, String cursor) throws IOException, ParseException { HttpTransport httpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = httpTransport .createRequestFactory(); JSONParser parser = new JSONParser(); GenericUrl url = new GenericUrl( "https://www.googleapis.com/freebase/v1/mqlread"); url.put("query", mqlQuery); url.put("key", apikey); url.put("cursor", cursor); logger.debug("QUERY URL: " + url.toString()); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); JSONObject response = (JSONObject) parser.parse(httpResponse .parseAsString()); return response; }
JSONParser parser = new JSONParser(); GenericUrl url = new GenericUrl("https://kgsearch.googleapis.com/v1/entities:search"); url.put("query", "Kolkata"); url.put("limit", "1"); url.put("indent", "true"); url.put("key", "xxxxxx"); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute();
public JSONObject getCursorAndResponse(String mqlQuery, String cursor) throws IOException, ParseException { HttpTransport httpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = httpTransport .createRequestFactory(); JSONParser parser = new JSONParser(); GenericUrl url = new GenericUrl( "https://www.googleapis.com/freebase/v1/mqlread"); url.put("query", mqlQuery); url.put("key", apikey); url.put("cursor", cursor); logger.debug("QUERY URL: " + url.toString()); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); JSONObject response = (JSONObject) parser.parse(httpResponse .parseAsString()); return response; }
public JSONObject getCursorAndResponse(String mqlQuery, String cursor) throws IOException, ParseException { HttpTransport httpTransport = new NetHttpTransport(); HttpRequestFactory requestFactory = httpTransport .createRequestFactory(); JSONParser parser = new JSONParser(); GenericUrl url = new GenericUrl( "https://www.googleapis.com/freebase/v1/mqlread"); url.put("query", mqlQuery); url.put("key", apikey); url.put("cursor", cursor); logger.debug("QUERY URL: " + url.toString()); HttpRequest request = requestFactory.buildGetRequest(url); HttpResponse httpResponse = request.execute(); JSONObject response = (JSONObject) parser.parse(httpResponse .parseAsString()); return response; }
private GenericUrl buildTargetUrl(String path) { GenericUrl genericUrl = new GenericUrl(translate.getBaseUrl() + "v2/" + path); if (options.getApiKey() != null) { genericUrl.put("key", options.getApiKey()); } return genericUrl; }
private String requestUploadLocation(java.io.File fileToUpload, String mimeType, HttpRequestFactory requestFactory, com.google.api.services.drive.model.File remoteFile) throws IOException { GenericUrl initializationUrl = new GenericUrl("https://www.googleapis.com/upload/drive/v2/files"); initializationUrl.put("uploadType", "resumable"); HttpRequest httpRequest = createHttpRequest(requestFactory, HttpMethods.POST, initializationUrl, new JsonHttpContent(DriveFactory.getJsonFactory(), remoteFile)); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.put("X-Upload-Content-Type", mimeType); httpHeaders.put("X-Upload-Content-Length", fileToUpload.length()); httpRequest.getHeaders().putAll(httpHeaders); LOGGER.log(Level.FINE, "Executing initial upload location request."); HttpResponse httpResponse = executeHttpRequest(httpRequest); if(!httpResponse.isSuccessStatusCode()) { throw new IOException("Request for upload location was not successful. Status-Code: " + httpResponse.getStatusCode()); } String location = httpResponse.getHeaders().getLocation(); LOGGER.log(Level.FINE, "URL for resumable upload: " + location); return location; }
/** Check that a response logs with the correct log. */ @Test public void testResponseLogs() throws IOException { new UploadIdResponseInterceptor().interceptResponse(buildHttpResponse("abc", null, "type")); GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL); url.put("uploadType", "type"); String worker = System.getProperty("worker_id"); expectedLogs.verifyDebug("Upload ID for url " + url + " on worker " + worker + " is abc"); } }
revokeUrl.put("token", revokeToken); HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory(); HttpRequest tokenRequest = requestFactory.buildGetRequest(revokeUrl);
/** * Builds a HttpResponse with the given string response. * * @param header header value to provide or null if none. * @param uploadId upload id to provide in the url upload id param or null if none. * @param uploadType upload type to provide in url upload type param or null if none. * @return HttpResponse with the given parameters * @throws IOException */ private HttpResponse buildHttpResponse(String header, String uploadId, String uploadType) throws IOException { MockHttpTransport.Builder builder = new MockHttpTransport.Builder(); MockLowLevelHttpResponse resp = new MockLowLevelHttpResponse(); builder.setLowLevelHttpResponse(resp); resp.setStatusCode(200); GenericUrl url = new GenericUrl(HttpTesting.SIMPLE_URL); if (header != null) { resp.addHeader("X-GUploader-UploadID", header); } if (uploadId != null) { url.put("upload_id", uploadId); } if (uploadType != null) { url.put("uploadType", uploadType); } return builder.build().createRequestFactory().buildGetRequest(url).execute(); }
/** * This method sends a POST request with empty content to get the unique upload URL. * * @param initiationRequestUrl The request URL where the initiation request will be sent */ private HttpResponse executeUploadInitiation(GenericUrl initiationRequestUrl) throws IOException { updateStateAndNotifyListener(UploadState.INITIATION_STARTED); initiationRequestUrl.put("uploadType", "resumable"); HttpContent content = metadata == null ? new EmptyContent() : metadata; HttpRequest request = requestFactory.buildRequest(initiationRequestMethod, initiationRequestUrl, content); initiationHeaders.set(CONTENT_TYPE_HEADER, mediaContent.getType()); if (isMediaLengthKnown()) { initiationHeaders.set(CONTENT_LENGTH_HEADER, getMediaContentLength()); } request.getHeaders().putAll(initiationHeaders); HttpResponse response = executeCurrentRequest(request); boolean notificationCompleted = false; try { updateStateAndNotifyListener(UploadState.INITIATION_COMPLETE); notificationCompleted = true; } finally { if (!notificationCompleted) { response.disconnect(); } } return response; }