发布谷歌融合表的请求

时间:2014-04-01 14:59:02

标签: java oauth-2.0 google-fusion-tables

我一直试图在我的google fusion表中插入新行,但作为回应我收到401错误;任何其他邮寄请求也不起作用。 我已经放了oauth访问令牌,还有api密钥,这里是代码:

private void sendPost() throws Exception {
             String url = "https://www.googleapis.com/fusiontables/v1/query?sql= **here I put my   query** ?key="+ "**here I put my api key**";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add request header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", "Chrome/11.0.660.0");
    con.addRequestProperty("client_id", "**here I put my client_ID**");
    con.addRequestProperty("client_secret", "**here I put my client_secret**");
    con.setRequestProperty("Authorization","Oauth " + "**here I put my access token**");
    con.setRequestProperty("Content-Type", "application/json");
    String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(newInputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());
    } 

}

你能告诉我代码是否错误以及在哪个方面?

提前谢谢。

1 个答案:

答案 0 :(得分:0)

需要两个单独的请求:

  • 第一个POST请求,以获取OAuth令牌。主体包含client_id,client_secret,redirect_uri,grant_type和authorization_code。
  • 第二个POST请求,正文是SQL命令。在这种情况下,INSERT INTO TableID(field1,field2,...)VALUES(value1,value2,...)。此请求需要Authentication标头令牌。