Google spreadsheet API, 400 error bad request : unable to parse range - java

I am trying to access Google spreadsheets using a spreadsheet example. When I run the example code it worked fine. I just change the SpreadsheetId and range. It started giving me:
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Unable to parse range: Class Data!A2:A4",
"reason" : "badRequest"
} ],
"message" : "Unable to parse range: Class Data!A2:A4",
"status" : "INVALID_ARGUMENT"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at poc.mainPOC.main(mainPOC.java:157)
Below is the code:
String spreadsheetId = "my spread sheet ID";
String range = "Class Data!A2:A4";
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();

Try replacing Class Data!A2:A4 with A2:A4

If you look at the sheet itself you will notice that the Worksheet is titled "Class Data". So just put the name of your sheet where is says "Class Data". Example:
String range = "SheetName!A1:C";

String range = "Class Data!A2:A4";
The Class Data is your worksheet's name, FYI: the name on the tab at the bottom, the default one is "Sheet1". Replace Class Data with the one you want to work with.

I was trying to add some data to a sheet named Emmett that did not existed yet and was receiving this error:
Error: Unable to parse range: Emmet!A2:C12
I had to manually create the sheet named Emmett in the spreadsheet and then it worked like a charm.

I ran into this error when I had a typo in the name of the tab. In your case "Class Data" didn't match the name of the tab

In my case there was an extra space in the google sheet while I was trimming the sheet name at my end. Once I removed the trimming logic, everything worked fine.

Something that I learned was that If the column name doesn't exist yet, this command won't be able to find it. You need to make sure the column you're writing to already exists -- I did this manually from the google sheet.

Related

GCP Label Detection with AEM returns 400 response

I am trying to use Google cloud vision label detection for aem assets. I am converting the assets into input stream in the following way:
AssetManager assetMgr = resolver.adaptTo(AssetManager.class);
Asset myAsset = assetMgr.getAsset(payload);
Rendition myRen = myAsset.getRendition(payload +
Constants.originalRendition);
InputStream is = myRen.getStream();
Once I get the stream, I hit the service this way
but I get the following response:
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Request must specify image and features.",
"reason" : "badRequest"
} ],
"message" : "Request must specify image and features.",
"status" : "INVALID_ARGUMENT"
}
Creating the InputStream same way for google face detect works fine.
It seems you are converting the images to a stream of bytes using the IOUtils.toByteArray() method. As the code is for detecting labels is very similar to the code to detecting faces, except for the part where you convert the image to a stream of bytes, and you say can detecting faces fine, I’d try converting the image using the class ByteString. This class is used in the code example in the documentation for detecting labels here.

Problem: Bing Imagesarch API returns image urls. Using ImageIO.read(url) to read returns null pointer exception

I´m using the Bing image search API on Netbeans java 8. I obtained the JSON data correctly, about 263 matches. Every match has its data on a "value" key, and each image is stored in "contentUrl" key.
For example:
value
contentUrl="http://www.example.com/images/cake-chocolate.jpg"
value
contentUrl="https://www.example2.com/images/chocolatecake.png"
...etc
I obtain the contentUrl as a String, then convert it using URL as the following example code:
String firstMatch = (String) firstMatch.get("contentUrl");
URL url1 = new URL(firstMatch);
Image cakePic = ImageIO.read(url1);
ImageIcon cakeIcon = new ImageIcon(cakePic);
Now the problems:
On my first 3 image matches, or "contentUrl" urls, I can only obtain and show the first match. The second and third throw a null pointer exception as follows:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
I printed the URL variables on system.out, of my own testing. I used the query "delicious salads anne wilson" (translated from spanish). I pasted those URLs on Firefox and the images showed no problems.
Of these first 3 matches, I can only load the 1st match. 2nd and 3rd throw null pointer exception. I tried each match alone and still getting null pointer exception.
http://globedia.com/imagenes/noticias/2011/12/17/deliciosas-ensaladas-anne-wilson_1_1014200.jpg
http://librosgratisxd.com/wp-content/uploads/2017/07/deliciosas-ensaladas-de-anne-wilson2.jpg
http://librosgratisxd.com/wp-content/uploads/2017/08/deliciosas-recetas-de-carne-picada-de-anne-wilson-150x150.jpg
I´m suspecting that ImageIO.read(URL) is missing something that the server needs. Maybe an useragent? a referer? I tried other of the 263 matches and some work and others don´t. Please help!
EDIT: added the second image match JSON data. This one throws a null pointer exception.
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?view=detailv2&FORM=OIIRPO&q=Deliciosas+ensaladas+-+Anne+Wilson.pdf&id=3BFE78A6DEFEEF3854FED2B49A902EC885D1037C&simid=607995057971464317",
"name":"Deliciosas ensaladas ( Anne Wilson)",
"thumbnailUrl":"https:\/\/tse3.mm.bing.net\/th?id=OIP.cB5jsoePgzfSdxEf8kDIUQDJEs&pid=Api",
"datePublished":"2012-01-08T05:52:00.0000000Z",
"isFamilyFriendly":true,
"contentUrl":"http:\/\/globedia.com\/imagenes\/noticias\/2011\/12\/17\/deliciosas-ensaladas-anne-wilson_1_1014200.jpg",
"hostPageUrl":"http:\/\/globedia.com\/deliciosas-ensaladas-anne-wilson",
"contentSize":"114736 B",
"encodingFormat":"jpeg",
"hostPageDisplayUrl":"globedia.com\/deliciosas-ensaladas-anne-wilson",
"width":344,
"height":512,
"hostPageFavIconUrl":"https:\/\/www.bing.com\/th?id=AR_6305881f91324e69f0fc25409f3f15c0&pid=Api",
"thumbnail":{
"width":204,
"height":304
},
"imageInsightsToken":"ccid_cB5jsoeP*mid_3BFE78A6DEFEEF3854FED2B49A902EC885D1037C*simid_607995057971464317*thid_OIP.cB5jsoePgzfSdxEf8kDIUQDJEs",
"insightsMetadata":{
"pagesIncludingCount":6,
"availableSizesCount":4
},
"imageId":"3BFE78A6DEFEEF3854FED2B49A902EC885D1037C",
"accentColor":"A33E28"
}
EDIT 2: I tried using apache commons IO library, and used the
FileUtils.copyURLToFile(url, file);
The file written was an html file with this content:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved here.</p>
</body></html>
Basically, its the same url but with "https" on the front. Damn.

How to fetch sub json from request parameter

I just decided to send below json data from client to server. Then i found all my previous request were not of type json. And i am unable to send json. Below is the json i want to send in data of jquery ajax.
data:{
id:"10",
sampleArr:[
{ id:"hello","sample":"hello"},
{ id:"hello1","sample":"hello1"}
]
}
and at server i get below parameters
id=10
group[0][id]=hello
group[0][sample]=hello
group[1][id]=hello1
group[1][sample]=hello1
so i am confused how to fetch all groups
One problem is that what you are sending is not valid JSON.
{ "data" : {
"id" : "10",
"sampleArr": [
{ "id" : "hello", "sample" : "hello"},
{ "id" : "hello1", "sample" : "hello1"}
]
}
}
Notice that all attribute names must be quoted, and the top-level JSON object must have curly brackets around it.
If that doesn't help, you need to explain how your servlet is receiving and parsing the JSON.
#BigMike, Thanks i am able to fetch complete json and play around with it. Was unable to send JSON even setting content type application/json. But Still checking why, but working as temperary fix (Might be that i am not using Rest API)

autodesk forge "Failed to trigger translation for this file"

I am trying to use the autodesk forge viewer tutorial
https://developer.autodesk.com/en/docs/model-derivative/v2/tutorials/prepare-file-for-viewer/
I have successfully uploaded and downloaded a dwg file
on the step where i convert it to svf it never seems to process and fails with
{"input":{"urn":"Safe Base64 encoded value of the output of the upload result"},"output":{"formats":[{"type":"svf","views":["2d","3d"]}]}}
HTTP/1.1 400 Bad Request
Result{"diagnostic":"Failed to trigger translation for this file."}
First question do i need to remove the urn: before Base64 encoding.
Second is there any more verbose error result that I can see.
Note I have also tried with a rvt file and tried with "type":"thumbnail" nothing seems to work.
I feel my Encoded URN is incorrect but I am not sure why it would be.
On the tutorial page they seem to have a much longer and raw urn not sure if I should be appending something else to it before encoding. they have a version and some other number
from tutorial
raw
"urn:adsk.a360betadev:fs.file:business.lmvtest.DS5a730QTbf1122d07 51814909a776d191611?version=12"
mine
raw
"urn:adsk.objects:os.object:gregbimbucket/XXX"
EDIT:
This is what i get back from the upload of a dwg file
HTTP/1.1 200 OK
Result{
"bucketKey" : "gregbimbucket",
"objectId" : "urn:adsk.objects:os.object:gregbimbucket/XXX",
"objectKey" : "XXX",
"sha1" : "xxxx",
"size" : 57544,
"contentType" : "application/octet-stream",
"location" : "https://developer.api.autodesk.com/oss/v2/buckets/gregbimbucket/objects/XXX"
}
This is what i send to convert the file
{"input":{"urn":"dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JlZ2JpbWJ1Y2tldC9YWFg"},"output":{"formats":[{"type":"svf","views":["2d","3d"]}]}}
This is the error I get back
HTTP/1.1 400 Bad Request
Result{"diagnostic":"Failed to trigger translation for this file."}
EDIT 2: SOLUTION
It looks like the object_id when uploading a file has to have the file extension and not end in a GUI or random set of characters for it to know what file type it is. So that it can be converted.
"objectId" : "urn:adsk.objects:os.object:gregbimbucket/Floor_sm.dwg",
SOLUTION It looks like the object_id when uploading a file has to have the file extension and not end in a GUI or random set of characters for it to know what file type it is.

How to do I locate a Google SpreadSheet ID?

https://developers.google.com/apps-script/guides/rest/api#parameter_and_return_types
When using the Google Execution Api for Java, the ID of the spreadsheet isn't very easy to understand. Here is the code snippet:
// Initialize parameters for that function.
String sheetId = "<ENTER_ID_OF_SPREADSHEET_TO_EXAMINE_HERE>";
List<Object> params = new ArrayList<Object>();
params.add(sheetId);
Any Ideas on how to locate this. I run the Sheets api to return the id of a SpreadSheet but it returns a link, and when I use the https://spreadsheets.google.com/feeds/spreadsheets/STRING or just the STRING itself I still get:
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Requested entity was not found.",
"reason" : "notFound"
Found this deep inside the Apps Script API reference.
A spreadsheet ID can be extracted from its URL. For example, the spreadsheet ID in the URL https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is "abc1234567".
https://developers.google.com/sheets/api/guides/concepts
states that:
Every API method requires a spreadsheetId parameter which is used to identify which spreadsheet is to be accessed or altered. This ID is the value between the "/d/" and the "/edit" in the URL of your spreadsheet. For example, consider the following URL that references a Google Sheets spreadsheet:
https://docs.google.com/spreadsheets/d/1qpyC0XzvTcKT6EISywvqESX3A0MwQoFDE8p-Bll4hps/edit#gid=0
The ID of this spreadsheet is 1qpyC0XzvTcKT6EISywvqESX3A0MwQoFDE8p-Bll4hps.
So I just pulled up my spreadsheet and looked at the URL to find my ID.
Hope this helps!
I just created a hyperlink to the sheets and then copied that hyperlink to a text file and the link line shows the GID.
https://docs.google.com/spreadsheets/d/1TxNHS6vuse1is2Mw_hUs9wTDM6f095Y6pKLeltUfNzQ/edit#gid=1049871492
Then I created a pull down list in A52 and then made =IF() logic to go to the GIDs to go to those sheets.
=IF(A52="47QTCK18D0001",HYPERLINK("#gid=0","47QTCK18D0001"),IF(A52="47QTCK18D0002",HYPERLINK("#gid=1049871492","47QTCK18D0002") ....

Categories