0

This is my HTTP URL

POST HTTPS://www.googleapis.com/admin/directory/v1/group
MY json request 
{
   "email": "sales_group@example.com",
   "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", "abc@gmail.com");
        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???

Bharathi
  • 451
  • 1
  • 6
  • 17

2 Answers2

1

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

Romin
  • 8,708
  • 2
  • 24
  • 28
  • 401 is due to the fact that you are unauthorized. Before you make a call to the API, you will need to login with the correct credentials that is allowed to execute that action. Please refer to the documentation for the specific API. – Romin Dec 11 '13 at 10:25
0

You need to add authorization header with Access Token

Authorization : Bearer ya29.vQE48oltidkR

jkb016
  • 439
  • 1
  • 7
  • 17