This is my HTTP URL
POST HTTPS://www.googleapis.com/admin/directory/v1/group
MY json request
{
"email": "[email protected]",
"name": "Sales Group",
"description": "This is the Sales group."
}
I am using Directory API to create groups.
I never used URL fetch so far so i am not familiar with that. Please help me how can i do that..
THIS IS MY ANSWER I POSTED AFTER 2 HOURS. stackoverflow did not allow me to to answer my own question before 8 hours since i have less than 10 reputation, so forgive me for long question. I tried this.. i was struggling a bit in passing json as parameter. here is my code
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Logger;
import javax.servlet.http.*;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
@SuppressWarnings("serial")
public class DirectoryApiExampleServlet extends HttpServlet {
static Logger log = gger.getLogger(DirectoryApiExampleServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
URL url = new URL("https://www.googleapis.com/admin/directory/v1/groups");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
JSONObject json = new JSONObject();
try {
json.put("email", "[email protected]");
json.put("name", "Test Group");
json.put("description", "this is a test group");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer.write(json.toString());
writer.flush();
writer.close();
log.info("connection.getResponseCode()"+connection.getResponseCode());
}
}
But it is giving 401 response which is not expected.
Where am i making mistake???
There is documentation available on how to make a POST request. An example is also provided. Check out the following section : https://developers.google.com/appengine/docs/java/urlfetch/usingjavanet#Using_HttpURLConnection