Google - API: Unterschied zwischen den Versionen
Aus Wikizone
| Zeile 9: | Zeile 9: | ||
https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/v2/reference/files/get | https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/v2/reference/files/get | ||
| + | Beispiel Code | ||
| + | <syntaxhighlight lang="javascript"> | ||
| + | ByteArrayContent mediaContent = new ByteArrayContent("text/html", "HTML PAGE HERE".getBytes()); | ||
| + | |||
| + | File body = new File(); | ||
| + | body.setTitle("test.html"); | ||
| + | body.setMimeType("text/html"); | ||
| + | |||
| + | Insert request = null; | ||
| + | try | ||
| + | { | ||
| + | request = service.files().insert(body, mediaContent); | ||
| + | request.setConvert(true); | ||
| + | File file = request.execute(); | ||
| + | |||
| + | HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getExportLinks().get("application/pdf"))).execute(); | ||
| + | |||
| + | OutputStream out = new FileOutputStream(getExternalFilesDir(null).getAbsolutePath() + "/test.pdf"); | ||
| + | byte[] buf = new byte[1024]; | ||
| + | int len; | ||
| + | while ((len = resp.getContent().read(buf)) > 0) | ||
| + | { | ||
| + | out.write(buf, 0, len); | ||
| + | } | ||
| + | out.close(); | ||
| + | |||
| + | } | ||
| + | catch (IOException e) | ||
| + | { | ||
| + | e.printStackTrace(); | ||
| + | } | ||
| + | </syntaxhighlight> | ||
Aktuelle Version vom 20. Juli 2020, 08:26 Uhr
Die Google API bietet mittlerweile eine Menge Tools die man nutzen kannn. Natürlich immer mit den üblichen Bedenken zur Datekrake Google. Man sieht allerdings auch immer mehr Services die die Google API als Teil ihrer Infrastruktur nutzen. So erzeugt Shopify hinter den Kulissen pdfs z.B. für Lieferscheine mittels Google API und zeigt sie dann direkt als Google Doc an.
API Bereiche[Bearbeiten]
Google Maps[Bearbeiten]
Google Maps API - Programmieren für Google Maps
Google Drive / Google Docs[Bearbeiten]
html2pdf mit Google Drive Ein pdf aus einer Seite kann man über die API erzeugen indem man eine HTML Datei als Google Doc in Google Drive importiert und dort über die Drive API konvertiert.
https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/v2/reference/files/get
Beispiel Code
ByteArrayContent mediaContent = new ByteArrayContent("text/html", "HTML PAGE HERE".getBytes());
File body = new File();
body.setTitle("test.html");
body.setMimeType("text/html");
Insert request = null;
try
{
request = service.files().insert(body, mediaContent);
request.setConvert(true);
File file = request.execute();
HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getExportLinks().get("application/pdf"))).execute();
OutputStream out = new FileOutputStream(getExternalFilesDir(null).getAbsolutePath() + "/test.pdf");
byte[] buf = new byte[1024];
int len;
while ((len = resp.getContent().read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}