我正在尝试使用put请求调用url,而url是
PUT https://www.googleapis.com/groups/v1/groups/groupUniqueId但是 得到错误响应为{“error”:{“errors”:[{“domain”: “global”,“reason”:“required”,“message”:“Required”}],“code”: 400,“消息”:“必需”}}
如果我打开链接
https://developers.google.com/google-apps/groups-settings/v1/reference/groups/update
然后有一个Try It Section我用OAuth2.0授权它并放置一些字段,如图像
我编写了一个用于调用此url的java servlet 这是它的doGet方法代码,我正在Google Appengine.i上编写它,授权使用oauth2和acess_token的应用程序是有效的,否则它将显示授权错误。 它显示了无效的值。
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();
}
}
我试图提出与图片相同的请求。 首先将关键参数设置为api key,然后通过调用setRequestMethod(“PUT”)将请求方法设置为PUT; 之后添加了两个属性,一个是Authorization:Bearer access_token 然后是Content-Type:application / json 然后通过调用方法
将其请求体设置为json对象connection1.getOutputStream().write(obj.toString().getBytes());
任何人都可以指出我错在哪里以及我的价值无效吗?
答案 0 :(得分:0)
因为我正在尝试更改群组的电子邮件地址,但是当我在群组页面中手动尝试时,它无法更改,这就是我收到400错误的原因