Search code examples
javagoogle-app-enginegoogle-apihttprequestgdata

Error 400 invalid value in google apps group settings api


I am trying to call a url with put request and the url is

PUT https://www.googleapis.com/groups/v1/groups/groupUniqueId but getting error response as { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Required" } ], "code": 400, "message": "Required" } }

if i open the link https://developers.google.com/google-apps/groups-settings/v1/reference/groups/update then there is a Try It Section i authorize it with OAuth2.0 and put some fields as shown in image enter image description here

i write a java servlet for calling this url here is its doGet Method code and i am writing it on Google Appengine.i authorized the applicaion with oauth2 and acess_token is valid else it will show Authorization Error. it showing me invalid values.

public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {

        String url = "https://accounts.google.com/o/oauth2/token";

        // FetchOptions opt = FetchOptions.Builder.doNotValidateCertificate();
           URL url1=new URL(url); 
           String param="code="+req.getParameter("code")+"&client_id=719226850877-tr21kp47phthuli0hu2v24akgfrplde9.apps.googleusercontent.com&client_secret=hSXi3dWfEmDQfl0XZJJmBssc&redirect_uri=https://www.onemoredemo.appspot.com/oauth2callback&grant_type=authorization_code";

             HttpURLConnection connection = 
        (HttpURLConnection)url1.openConnection(); 
             connection.setDoOutput(true); 
             connection.setRequestMethod("POST"); 
             connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
             connection.getOutputStream().write( param.getBytes() ); 
          InputStream str= connection.getInputStream();
          BufferedReader reader=new BufferedReader(new InputStreamReader(str));
        String l="";
        String l1="";

          while((l=reader.readLine())!=null){
              l1+=l;
             resp.getWriter().println(l);

          }
          try {
              JSONObject obj=new JSONObject();
              obj.put("email", "testing9@iritesh.com");
            JSONObject json=new JSONObject(l1);
            String access_token=json.getString("access_token");
            URL url2=new URL("https://www.googleapis.com/groups/v1/groups/testing@iritesh.com?key=AIzaSyATDZHO5J7a3ItTSBhPby__Xzxi7rZlMJQ");
            HttpURLConnection connection1=(HttpURLConnection) url2.openConnection();
            connection1.setRequestMethod("PUT");
            connection1.addRequestProperty("Content-Type", "application/json");
            connection1.setDoOutput(true);
            connection1.addRequestProperty("Authorization","Bearer "+ access_token);
            connection1.getOutputStream().write(obj.toString().getBytes());
            connection1.connect();
            InputStream str1= connection1.getInputStream();
              BufferedReader reader1=new BufferedReader(new InputStreamReader(str1));
             String rit;
              while( (rit=reader1.readLine())!=null)
             {resp.getWriter().println(rit);

             }


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

I tried to make a request same as in image. first set key parameter to api key then setting request method as PUT by invoking setRequestMethod("PUT"); after that added two properties one is Authorization:Bearer access_token and then Content-Type : application/json then setting its request body as a json object by invoking method

connection1.getOutputStream().write(obj.toString().getBytes());

Can any one please point me where i am wrong and where i am putting invalid value?


Solution

  • because i am trying to change the email address of the group but when i tried manually in groups page it cant be changes that's why i am getting 400 error