Automatize back up google cloud sql
I want to automatize google cloud export(i wanna do automatic back up). I
use SQLAdmin API(Experimental) and i copy their request for ask their
webservice.
This step its ok ,but when i want add a cron, i can't use sendRedirect or
similars for recover oauth token and let it to my other webservice, so in
one web service i have the request for automatize back ups, and the other
has the oauth method, but i can't connect them i'm out of ideas, can you
help me?
my code:
public class EjemploPruebaServlet extends HttpServlet {
private static final Logger log =
Logger.getLogger(EjemploPruebaServlet.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
GoogleClientSecrets secrets = new GoogleClientSecrets();
Details details = new Details();
details.setClientId("749918399993-mgd6bjcruk1b3kf0o5fj63nokt69iue9.apps.googleusercontent.com");
details.setClientSecret("xvMUoQmlwOyVwX2bCPZxqYoX");
secrets.setInstalled(details);
String url = new
GoogleBrowserClientRequestUrl(secrets,"http://77.ipejerciciodesarrollogratis.appspot.com/inbox",
Arrays.asList(
"https://www.googleapis.com/auth/cloud-platform")).setState("/profile").build();
resp.sendRedirect(url);
}
}
This is my oauth generator servlet.
public class Inbox extends HttpServlet {
private static final Logger log =
Logger.getLogger(Inbox.class.getName());
public void doGet(HttpServletRequest req, HttpServletResponse
resp) throws IOException {
URL url;
HttpURLConnection connection = null;
try {
if (req.getParameter("access_token") == null) {
resp.getWriter()
.println(
"<script type='text/javascript'> var
str=window.location.href; var
n=str.replace('#','?');document.write(n);");
resp.getWriter().println("location.href=n;
</script>");
}
//Create connection
url = new
URL("https://www.googleapis.com/sql/v1beta1/projects/intelligencepartner.com%3Aonohat-preprod/instances/ono-hat-vv2/export?key=AIzaSyAzRvLMQUJEmbP4GIIl2Y3F8xBXJ9ccqrY");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json");
//Este token está a pelo, es decir si se regenera morirá
la aplicación.
//Se puede mejorar cogiendo el token direcamente que
genera EjemploPruebaServlet, habría que hacer
//una llamada desde EjemploPruebaServlet a este servlet,
que haga lo de aqui pero que en
//Bearer cogamos el type_token y en lo de al lado
access_token (preguntar a vlopez en caso de duda)
connection.setRequestProperty("Authorization",
"Bearer" + " " +
"ya29.AHES6ZQqoGP1Sb16kzePmNNRfaYbrXX1x-gjt-7v_TrQSIjJ")
;
connection.setRequestProperty("X-JavaScript-User-Agent",
"Google APIs Explorer");
//connection.connect();
byte[] outputBytes = "{ 'exportContext': { uri:
'gs://pruebascsv/pruebaDefinitivaEsperoConCronFinish.gz',
kind: 'sql#exportContext' }}".getBytes("UTF-8");
DataOutputStream os = new
DataOutputStream(connection.getOutputStream());
os.write(outputBytes);
os.flush();
os.close();
int codigoRespuesta = (connection.getResponseCode());
log.info(String.valueOf(codigoRespuesta));
} catch (Exception e) {
log.info("Error, algo va mal");
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
And this is my request to google cloud sql api
If you need more information say me,
thank you!
Regards
No comments:
Post a Comment