我正在尝试使用 put 请求调用 url,而 url 是
PUT https://www.googleapis.com/groups/v1/groups/groupUniqueId但得到错误响应为 { "error": { "errors": [ { "domain": "global", "reason": "required" ,“消息”:“必需”} ],“代码”:400,“消息”:“必需”} }
如果我打开链接 https://developers.google.com/google-apps/groups-settings/v1/reference/groups/update 则有一个 Try It 部分,我使用 OAuth2.0 对其进行授权并放置一些字段,如图所示在图像中
我编写了一个用于调用此 url 的 java servlet,这是它的 doGet 方法代码,我正在 Google Appengine 上编写它。我使用 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();
}
}
我试图提出与图像相同的请求。首先将 key 参数设置为 api key,然后通过调用 setRequestMethod("PUT") 将请求方法设置为 PUT;之后添加了两个属性,一个是 Authorization:Bearer access_token ,然后是 Content-Type : application/json 然后通过调用方法将其请求主体设置为 json 对象
connection1.getOutputStream().write(obj.toString().getBytes());
谁能指出我错在哪里以及我在哪里放置了无效值?