I want to create a new Folder in Google drive, here is my code:
OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/drive/v2/files");
request.addBodyParameter("title", "Folder");
request.addBodyParameter("mimeType", "application/vnd.google-apps.folder");
service.signRequest(accessToken, request);
Response response = request.send();
I'm getting 401 Unauthorized doing this way. If i take out the addBodyParameter lines, it works, but is created a Untitled file.
I don't know what is wrong, maybe are missing some lines of code, anyone knows how to fix this problem? Thanks in advance.
I've fixed it:
OAuthRequest request = new OAuthRequest(Verb.POST, "https://www.googleapis.com/drive/v2/files");
JSONObject res = new JSONObject();
res.put("title", "Folder");
res.put("mimeType", "application/vnd.google-apps.folder");
request.addPayload(res.toJSONString());
request.addHeader("Content-Length", ""+res.size());
request.addHeader("Content-Type", "application/json");
service.signRequest(accessToken, request);
Response response = request.send();
Had to put the title and the mimeType in a JSONObject, to add it as a Payload to the request, and add to the header of request, the content-lenght and content-type, of the json. Now it's working fine.
I hope it helps someone else later.