0

Is there any way to write text into an existing shared Google Document, or to create a new google file in a shared Google folder, using Java (and without connecting to any account, as documents and folders are shared) ?

Thanks !


*EDIT

Rather than using the Google API, I created a Google Form which is filled by the application and which is exported to a Google Sheet automatically. I'm able to do a "POST" Http request OR to open the pre-filled Google form :

try{URL url = new URL(my_google_form_direct_url);

    //PREPARE PARAMS
    Map<String,Object> params = new LinkedHashMap<>();
    params.put("entry." + id_1, "TEXT1");
    params.put("entry." + id_2, "TEXT2");
    StringBuilder postData = new StringBuilder();
    for(Map.Entry<String,Object> param : params.entrySet()){
        if(postData.length() != 0){postData.append('&');}
        postData.append(URLEncoder.encode(param.getKey(), StandardCharsets.UTF_8.name()));
        postData.append('=');
        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), StandardCharsets.UTF_8.name()));}
    byte[] postDataBytes = postData.toString().getBytes(StandardCharsets.UTF_8.name());

/************************************/        
    //SEND POST
    HttpURLConnection conn = (HttpURLConnection)url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
    conn.setDoOutput(true);
    conn.getOutputStream().write(postDataBytes);

    //GET RESPONSE
    int response = conn.getResponseCode();
    if(response == HttpURLConnection.HTTP_OK) {
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8.name()));
        InputStream in = conn.getInputStream();
        in.close();}
    conn.disconnect();

/************************************/
    //OR OPEN THE PRE-FILLED FORM
    URL prefilled_url = new URL(my_google_form_user_url + "?usp=pp_url&" + postData);
    if(Desktop.isDesktopSupported()){
        Desktop.getDesktop().browse(prefilled_url.toURI());}

}catch (IOException e1){e1.printStackTrace();}

But I was wondering if there is a limit for the length of the URL, when I open it with "Desktop" and when I do a POST request ?

Thanks!

Deaudouce
  • 167
  • 2
  • 12
  • Something like this? https://developers.google.com/sheets/api/guides/concepts – HRgiger Mar 13 '17 at 00:21
  • Try using API keys. Note that it is only applicable for public documents and it must send an OAuth 2.0 token along with the request (user consent) whenever your application requests private user data. Check the documentation [Authorize Requests](https://developers.google.com/sheets/api/guides/authorizing) to be familiarize how this works. – Mr.Rebot Mar 13 '17 at 22:16
  • Thanks ! I will take a look ! – Deaudouce Mar 13 '17 at 22:53
  • I edited the post to explain my workaround ! – Deaudouce Apr 24 '17 at 20:13

1 Answers1

0

you may use my pet project to submit Google forms through Java:

https://github.com/stepio/jgforms

Probably limits are mostly defined by HTTP standards, so this question may help you:

What is the maximum length of a URL in different browsers?

As per my experience with limits, I successfully used forms to implement a "logger" for Android app: got complete stack-traces logged along with explanatory messages.

stepio
  • 865
  • 2
  • 9
  • 22