Google App engine end points in Java - java

For one of my Android app, I had to do a simple compute from Backend. So without much research I ended up using Google AppEngine (Endpoints), since it was simple and could be easily built with my Android app (in Android Studio itself). The end point API is a very simple low compute API which will be called maybe a few hundred times a day.
Now I got the rudest shock of my life.
I got a bill of 500$ for Google App Engine, and I was billed for over 8600 hours for 30 days period. Even if I ran my server for 30x24 hours it would be 720 hours.
How is this possible?
What did I do wrong?
/**
* An endpoint class we are exposing
*/
#Api(
name = "pushApi",
version = "v3",
namespace = #ApiNamespace(
ownerDomain = "com.example.sample",
ownerName = "com.example.sample",
packagePath = ""
)
)
public class MyEndpoint {
PushBean response = new PushBean();
/**
* A simple endpoint method that takes a name and says Hi back
*/
#ApiMethod(name = "pushToTopic")
public PushBean toTopic(#Named("topic") String topic, #Named("dataJson") String dataJson) {
try{
response.messageTopic(topic,dataJson);
return response;
}catch(Exception e){
return null;
}
}
}
The above code is the entry class for the Endpoint.

Related

How to get Google Ads impressions based on location

I am working with Google Ads API. Based on my use case, I need to get the impressions, clicks, and other statistics about the products based on location(Without creating the campaign).
I contacted the Google Ads API team to address the issue but the doc reference they provided me didn't work in my case. (I'm not using keywords also)
generate forecast metrics
And also the query I used to access the data is...
public void queryTest(long customerId,String campaignId) {
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
SearchGoogleAdsStreamRequest request =
SearchGoogleAdsStreamRequest.newBuilder()
.setCustomerId(Long.toString(customerId))
.setQuery("SELECT metrics.clicks, metrics.impressions,geographic_view.country_criterion_id FROM geographic_view WHERE geographic_view.country_criterion_id = 1009919")
.build();
ServerStream<SearchGoogleAdsStreamResponse> stream =
googleAdsServiceClient.searchStreamCallable().call(request);
for (SearchGoogleAdsStreamResponse response : stream) {
for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
System.out.println("outPut"+googleAdsRow.getGeographicView());
}
}
}
}
Can someone please help me to resolve this issue??
Thank you!!

Getting "Task is not yet complete error" when reading data using google fit android api

I am learning Android, and my task is to retrieve steps data using Google Fit steps API. However, I keep getting "Task is not yet complete" error. I am not sure
This is my code for that part:
private List<Fitness> readHistoryDataFromGoogle() {
DataReadRequest readRequest = createDataReadRequest();
Task<DataReadResponse> response = com.google.android.gms.fitness.Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this)).readData(readRequest);
DataReadResponse responses = response.getResult();
List <Fitness> fitnessItems = parseFitness(responses);
return fitnessItems;
}
private List<Fitness> parseFitness(DataReadResponse dataReadResponse) {
List<com.example.mentalhealthapp.Fitness> fitnessItems = new ArrayList<>();
for (Bucket bucket : dataReadResponse.getBuckets()) {
for (DataSet set : bucket.getDataSets()) {
for (DataPoint dataPoint : set.getDataPoints()) {
int numSteps = dataPoint.getValue(Field.FIELD_STEPS).asInt();
long startDateTime = dataPoint.getStartTime(TimeUnit.MILLISECONDS);
long endDateTime = dataPoint.getEndTime(TimeUnit.MILLISECONDS);
fitnessItems.add(new com.example.mentalhealthapp.Fitness(numSteps, startDateTime, endDateTime));
}
}
}
return fitnessItems;
}
Thank you very much for your help!
You're getting this problem, because API requests (service calls) are Asynchronous, they don't complete and return data immediately and you can't run them line by line like other code. you have to WAIT for that response to complete before you can access the data it returns.
from the documentation it tells you it's a REST api:
Brief look at documentation
https://developers.google.com/android/reference/com/google/android/gms/fitness/RecordingClient suggests you should look at this for displaying data when it becomes available.

Java Google AutoML NLP client waiting forever for response (no exception thrown)

I trained succesfully my own NLP AutoML model yesterday. I am able to do quite accurate predictions in GCP console. Everything ran smoothly. Today I have been trying to do prediction from Java client based on this example https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/language/automl/src/main/java/com/google/cloud/language/samples/PredictionApi.java
I use correct projectId and modelId that I copied from GCP console but I am waiting for result forever. Even after couple of minutes there is still no response. There is no exception thrown. I use europe-west3 as computeRegion.
Strange thing is that I also use Java client for Google NLP Sentiment Analysis and it works without problems and returns response immediately (based on this example https://github.com/GoogleCloudPlatform/java-docs-samples/blob/master/language/cloud-client/src/main/java/com/example/language/QuickstartSample.java)
Both clients are connected to the same GCP project (have the same projectId) but only one of them is working properly.
Do you please have some clue what could be wrong?
Thank you in advance for any hints
This is the code:
public class PredictionApi {
public static void main(String[] args) throws IOException {
PredictionApi predictionApi = new PredictionApi();
predictionApi.predict("projectId", "us-central1", "modelId");
}
private void predict(String projectId, String computeRegion, String modelId) throws IOException {
PredictionServiceClient predictionClient = PredictionServiceClient.create();
ModelName name = ModelName.of(projectId, computeRegion, modelId);
String content = "BERLIN Germany and China want to sign two agreements to deepen their cooperation in the financial sector later this week a German government document seen by Reuters showed on Wednesday";
TextSnippet textSnippet =
TextSnippet.newBuilder().setContent(content).setMimeType("text/plain").build();
ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build();
Map<String, String> params = new HashMap<String, String>();
PredictResponse response = predictionClient.predict(name, payload, params);
System.out.println("Prediction results:");
for (AnnotationPayload annotationPayload : response.getPayloadList()) {
System.out.println("Predicted Class name :" + annotationPayload.getDisplayName());
System.out.println(
"Predicted Class Score :" + annotationPayload.getClassification().getScore());
}
}
}
europe-west3 is not supported. All trained automl models are currently served in us-central1. You should in theory receive some error like what you reported in another stackoverflow post. I am a bit surprised you didn't receive any error message from the server. Do you mind share your client side code?

Facebook marketing API custom website audience targeting using API

I am using Facebook marketing API v2.8 (JAVA preferred)
I am trying to creating custom website audiences using Facebook pixel by using the below rule :
String rule = "{\"url\":{\"eq\":\"http://www.example.com/abc.php\"}}"
public static CustomAudience createWebsitePixelCustomAudience(AdAccount adAccount, String pixelId,
String audienceName, EnumSubtype subtype, String rule) throws
APIException {
CustomAudience customAudience = adAccount.createCustomAudience()
.setPixelId(pixelId)
.setName(audienceName)
.setSubtype(CustomAudience.EnumSubtype.VALUE_WEBSITE)
.setRetentionDays(15L)
.setRule(rule)
.setPrefill(true)
.execute();
return customAudience;
}
What I want to achieve?
I want to target these audiences created above using API (Java preferred).
I tried to do this using this below code by passing Id of custom audiences created above.
public static Targeting targetAudience(List<String> countrylist, String customAudienceID) {
TargetingGeoLocation geoLocation = new TargetingGeoLocation()
.setFieldCountries(countrylist);
Targeting targeting = new Targeting()
.setFieldGeoLocations(geoLocation)
.setFieldCustomAudiences(customAudienceID);
return targeting;
}
I used the customAudienceID that I created at the start and passed it to above function.
But I am getting this error : Expected BEGIN_ARRAY but was NUMBER at line 1 column 14 path
Is this the correct way of linking custom website audience to AdSet?
Thanks ?
Finally I find a way to do it :
public static Targeting targetCustomAudience(List<String> countrylist, String customAudienceID) {
TargetingGeoLocation geoLocation = new TargetingGeoLocation()
.setFieldCountries(countrylist);
/**
* add geo-location to targeting
* add custom audienceId to targeting
*/
Targeting targeting = new Targeting()
.setFieldGeoLocations(geoLocation)
.setFieldCustomAudiences("[{id:" + customAudienceID + "}]");
return targeting;
}
Thanks to this link
In short add this line "[{id:" + customAudienceID + "}]" to add custom audience to targeting.

search google and get results using java swing [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Does anyone know if and how it is possible to search Google programmatically - especially if there is a Java API for it?
Some facts:
Google offers a public search webservice API which returns JSON: http://ajax.googleapis.com/ajax/services/search/web. Documentation here
Java offers java.net.URL and java.net.URLConnection to fire and handle HTTP requests.
JSON can in Java be converted to a fullworthy Javabean object using an arbitrary Java JSON API. One of the best is Google Gson.
Now do the math:
public static void main(String[] args) throws Exception {
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String search = "stackoverflow";
String charset = "UTF-8";
URL url = new URL(google + URLEncoder.encode(search, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
// Show title and URL of 1st result.
System.out.println(results.getResponseData().getResults().get(0).getTitle());
System.out.println(results.getResponseData().getResults().get(0).getUrl());
}
With this Javabean class representing the most important JSON data as returned by Google (it actually returns more data, but it's left up to you as an exercise to expand this Javabean code accordingly):
public class GoogleResults {
private ResponseData responseData;
public ResponseData getResponseData() { return responseData; }
public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
public String toString() { return "ResponseData[" + responseData + "]"; }
static class ResponseData {
private List<Result> results;
public List<Result> getResults() { return results; }
public void setResults(List<Result> results) { this.results = results; }
public String toString() { return "Results[" + results + "]"; }
}
static class Result {
private String url;
private String title;
public String getUrl() { return url; }
public String getTitle() { return title; }
public void setUrl(String url) { this.url = url; }
public void setTitle(String title) { this.title = title; }
public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
}
}
###See also:
How to fire and handle HTTP requests using java.net.URLConnection
How to convert JSON to Java
Update since November 2010 (2 months after the above answer), the public search webservice has become deprecated (and the last day on which the service was offered was September 29, 2014). Your best bet is now querying http://www.google.com/search directly along with a honest user agent and then parse the result using a HTML parser. If you omit the user agent, then you get a 403 back. If you're lying in the user agent and simulate a web browser (e.g. Chrome or Firefox), then you get a way much larger HTML response back which is a waste of bandwidth and performance.
Here's a kickoff example using Jsoup as HTML parser:
String google = "http://www.google.com/search?q=";
String search = "stackoverflow";
String charset = "UTF-8";
String userAgent = "ExampleBot 1.0 (+http://example.com/bot)"; // Change this to your company's name and bot homepage!
Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().select(".g>.r>a");
for (Element link : links) {
String title = link.text();
String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");
if (!url.startsWith("http")) {
continue; // Ads/news/etc.
}
System.out.println("Title: " + title);
System.out.println("URL: " + url);
}
To search google using API you should use Google Custom Search, scraping web page is not allowed
In java you can use CustomSearch API Client Library for Java
The maven dependency is:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-customsearch</artifactId>
<version>v1-rev57-1.23.0</version>
</dependency>
Example code searching using Google CustomSearch API Client Library
public static void main(String[] args) throws GeneralSecurityException, IOException {
String searchQuery = "test"; //The query to search
String cx = "002845322276752338984:vxqzfa86nqc"; //Your search engine
//Instance Customsearch
Customsearch cs = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null)
.setApplicationName("MyApplication")
.setGoogleClientRequestInitializer(new CustomsearchRequestInitializer("your api key"))
.build();
//Set search parameter
Customsearch.Cse.List list = cs.cse().list(searchQuery).setCx(cx);
//Execute search
Search result = list.execute();
if (result.getItems()!=null){
for (Result ri : result.getItems()) {
//Get title, link, body etc. from search
System.out.println(ri.getTitle() + ", " + ri.getLink());
}
}
}
As you can see you will need to request an api key and setup an own search engine id, cx.
Note that you can search the whole web by selecting "Search entire web" on basic tab settings during setup of cx, but results will not be exactly the same as a normal browser google search.
Currently (date of answer) you get 100 api calls per day for free, then google like to share your profit.
In the Terms of Service of google we can read:
5.3 You agree not to access (or attempt to access) any of the Services by any means other than through the interface that is provided by Google, unless you have been specifically allowed to do so in a separate agreement with Google. You specifically agree not to access (or attempt to access) any of the Services through any automated means (including use of scripts or web crawlers) and shall ensure that you comply with the instructions set out in any robots.txt file present on the Services.
So I guess the answer is No. More over the SOAP API is no longer available
Google TOS have been relaxed a bit in April 2014. Now it states:
"Don’t misuse our Services. For example, don’t interfere with our Services or try to access them using a method other than the interface and the instructions that we provide."
So the passage about "automated means" and scripts is gone now. It evidently still is not the desired (by google) way of accessing their services, but I think it is now formally open to interpretation of what exactly an "interface" is and whether it makes any difference as of how exactly returned HTML is processed (rendered or parsed). Anyhow, I have written a Java convenience library and it is up to you to decide whether to use it or not:
https://github.com/afedulov/google-web-search
Indeed there is an API to search google programmatically. The API is called google custom search. For using this API, you will need an Google Developer API key and a cx key. A simple procedure for accessing google search from java program is explained in my blog.
Now dead, here is the Wayback Machine link.
As an alternative to BalusC answer as it has been deprecated and you have to use proxies, you can use this package. Code sample:
Map<String, String> parameter = new HashMap<>();
parameter.put("q", "Coffee");
parameter.put("location", "Portland");
GoogleSearchResults serp = new GoogleSearchResults(parameter);
JsonObject data = serp.getJson();
JsonArray results = (JsonArray) data.get("organic_results");
JsonObject first_result = results.get(0).getAsJsonObject();
System.out.println("first coffee: " + first_result.get("title").getAsString());
Library on GitHub
In light of those TOS alterations last year we built an API that gives access to Google's search. It was for our own use only but after some requests we decided to open it up. We're planning to add additional search engines in the future!
Should anyone be looking for an easy way to implement / acquire search results you are free to sign up and give the REST API a try: https://searchapi.io
It returns JSON results and should be easy enough to implement with the detailed docs.
It's a shame that Bing and Yahoo are miles ahead on Google in this regard. Their APIs aren't cheap, but at least available.

Categories

Resources