Reputation: 1439
In my Java web application I need to show videos stored in a Google Drive account to unauthenticated users.
I managed to solve this using OAuth2 from Google API in server application to retrieve an access token and the DownloadUrl attribute of the video file. Then adding the access token to the URL this way:
https://doc-10-8g-docs.googleusercontent.com/...&access_token=XXXXXXXX
I pass this URL to the front-end application, either a HTML5 video player or Flash video player, and it works.
But I'd like to know if it's possible to use Google Drive video player in a similar way, for few reasons:
I tried to use EmbedLink from Drive API with access_token parameter in a similar way but it doesn't work.
Alternative solution could be to play real streaming in the Flash/HTML5 player. That should be enough for mi purpose.
Upvotes: 2
Views: 3408
Reputation: 1439
I found the solution myself, by looking the requests Google Drive embed player does.
Basically you make a request for this address:
https://docs.google.com/get_video_info?authuser=&docid=<YOUR_VIDEO_ID>
You can do it using Drive client after OAuth authentication or attach &access_token=YOUR_TOKEN
at the end of the URL. Both options work.
The response consists in URL Encoded pairs of field and value, with info about the video. The interesting field for our porpuse is fmt_stream_map
, which contains a list of available streams in various formats and codecs. These URL can be send to a Flash or HTML5 Player, which will reproduce it without authentication needed.
If you want to start the video in any specific moment, just add &begin=MILLISECONDS
at the end of the stream URL.
NOTE: You can get the URL list from the field url_encoded_fmt_stream_map
. It's a little more difficult to parse, but contains other useful information like video mime type and codecs.
An example code:
Credential credential = getOAuth2Credential(); //Your authentication stuff
//Create a new authorized API client
Drive service = new Drive.Builder(new NetHttpTransport(), new JacksonFactory(), credential).build();
String fileId = "XXXXXXXXXXX"; //Your video docid in Google Drive
HttpResponse resp = service.getRequestFactory()
.buildGetRequest(new GenericUrl("https://docs.google.com/get_video_info?authuser=&docid=" + fileId)).execute();
//Convert response InputStream to String
String content = "";
InputStreamReader isr = new InputStreamReader(resp.getContent());
int ch = 0;
while (ch != -1) {
ch = isr.read();
if (ch != -1) content += (char)ch;
}
//Split response in pairs field / value
StringTokenizer st = new StringTokenizer(content, "&");
while (st.hasMoreTokens()) {
String token = st.nextToken();
if (token.split("=").length == 2) {
String field = token.split("=")[0];
String value = URLDecoder.decode(token.split("=")[1], "UTF-8");
if ("url_encoded_fmt_stream_map".equals(field)) {
//Prints info of each available video Stream
String[] urlList = value.split(",");
for (int i=0; i < urlList.length; i++) {
System.out.println("Stream #" + i + ":");
System.out.println("URL: " + URLDecoder.decode(urlList[i].split("&")[1].split("=")[1], "UTF-8"));
System.out.println("Quality: " + URLDecoder.decode(urlList[i].split("&")[3].split("=")[1], "UTF-8"));
String type = URLDecoder.decode(urlList[i].split("&")[2], "UTF-8");
if (type.indexOf(';') > 0) {
System.out.println("Mime type: " + type.substring(5, type.indexOf(';')));
System.out.println("Codecs: " + type.substring(type.indexOf("codecs=\"") + 8, type.lastIndexOf('"')));
}
else {
System.out.println("Mime type: " + type.substring(5));
}
}
}
}
}
Upvotes: 1