-1

I have an application hosted in heruko, a website.

I want the users to be able to put some text in the website(using input text or something), and then I want to take this text, create from it a .txt file, and then upload it to my google drive.

I have server side written in java. It looks like :

@WebServlet( name = "uploadServlet", urlPatterns = {"/uploadFile"} )

public class uploadFIleServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    ServletOutputStream out = resp.getOutputStream();
    out.write("hello omer you won".getBytes());

    File fileMetadata = new File();
    fileMetadata.setName("omer.txt");
    java.io.File filePath = new java.io.File("omer.txt");
    FileContent mediaContent = new FileContent("image/jpeg", filePath);
    ....
    //Send file to my google drive account using my email and password.
    //Send file to my google drive account using my email and password.
    //Send file to my google drive account using my email and password.
    //Send file to my google drive account using my email and password.
    //Send file to my google drive account using my email and password.
    //Send file to my google drive account using my email and password.
    ....
    System.out.println("File ID: " + file.getId());

    out.flush();
    out.close();
}

}

When the client asks for /uploadFile url, the "doGet" function that mentioned above is executed. So far so good.

Then, I dont know what code will do what I'm looking for, because everything I found in google documentation asks the users to log in to their google drive, what I don't want to happen.

I want the users to be able to just put text in my app, so it will be sent to my google drive account as .txt file.

I dont care putting my email and password in the java code!

So, how can I do it?

Thank you!

OmerLevi
  • 119
  • 1
  • 8

1 Answers1

1

What is the problem precisely? If you have this file in your method, create some object that will take care of making http requests. Post your text file to google drive, using your credentials. You can find a lot of good libraries for making http request, mine for example: https://github.com/Iprogrammerr/Gentle-Request Pseudocode, using my library:

ServletOutputStream out = resp.getOutputStream();
out.write("hello omer you won".getBytes());

File fileMetadata = new File();
fileMetadata.setName("omer.txt");
java.io.File filePath = new java.io.File("omer.txt");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
Connections connections = new HttpConnections();
Response response = connections.response(new PostRequest("google-url", new AuthorizationHeader("whatever google want to authorize your account"), fileMetadata);

System.out.println("File ID: " + file.getId());

out.flush();
out.close();
Binary Igor
  • 168
  • 1
  • 5
  • My problem is that every thing I've tried, asks the users to login to their Google drive account, and then only they can post it to mine. Can you give me a piece of code that will do what you told, without asking the user for any details or any authentication? I want just to take their text without Google drive account at all, I want this to work even if the client does not have Google drive account. – OmerLevi Nov 26 '18 at 04:37
  • You said that you have content of this file from some form on your website and that all you have to do is to give credentials to YOUR Google drive. If this is the case, I have edited my answer. – Binary Igor Nov 26 '18 at 21:27
  • Oh dude you almost understood me. We are very close. I do have a content in some form, but the problem is not taking the data from the form. The problem is after I took the data, and created a file from it, how do I upload it to my drive account. So in my question, I talked about any file, because making the file from the form text is not my problem, my problem is *how to upload a ready file to my specific Google drive account*. So now I think your answer is good, but I didn't understand what to fill in the "google-url", and in the "whatever Google"... So what should I put there? Thanks – OmerLevi Nov 27 '18 at 05:25
  • Url to your google drive. In authorization header your put either some token and login/password from your google account. You have to check their documentation to know what they want, I do not know. – Binary Igor Nov 27 '18 at 21:07