1. Following is the callback method to be called from createPicker method in Javascript
function onPickerAction(data) {
if (data.action === google.picker.Action.PICKED) {
var id = data.docs[0].id;
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
var linkTag = document.getElementById('link');
linkTag.href = url;
linkTag.textContent = doc.name;
document.getElementById("fileName").value = doc.name;
var request = new XMLHttpRequest();
request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + id);
request.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token);
request.addEventListener('load', function () {
var item = JSON.parse(request.responseText);
alert(request.responseText);
document.getElementById('fileLink').value = item.downloadUrl;
console.log(item);
});
request.send();
}
}
2. Following is the HTML code we need to add to pass the url to controller
<div id="result"></div>
<input type="image" src="~/Content/google_drive_logo.jpg" width="150px" height="100px" onclick=" onApiLoad() "/>
<a id="link" target="_blank"></a>
<div>
@using (Html.BeginForm("downloadFile", "GoogleDrive", null, FormMethod.Post))
{
<input type="hidden" name="_fileResourceDownloadUrl" id="fileLink" />
<input type="hidden" name="fileNameToSave" id="fileName"/>
<input type="submit" value="Submit Link"/>
}
</div>
3. Following is the code in controller for creating an AuthenticateOauth
public static DriveService AuthenticateOauth(string clientId, string clientSecret, string userName)
{
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveAppdata, DriveService.Scope.DriveAppsReadonly, DriveService.Scope.DriveFile, DriveService.Scope.DriveMetadataReadonly, DriveService.Scope.DriveReadonly, DriveService.Scope.DriveScripts };
try
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("FOLDER_NAME_TO_STORE_AUTHENTICATION_TOKEN_ON_SERVER")).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "APPL_NAME_AS_PER_API_CONSOLE",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;
}
}
4. Following is the method to downloadFile to application server
[HttpPost]
public ActionResult downloadFile(string _fileResourceDownloadUrl,string fileNameToSave)
{
string CLIENT_ID = "CLIENT_ID";
string CLIENT_SECRET = "CLIENT_SECRET";
string MY_EMAIL_ADD = "EMAIL_FROM_DEVELOPER_CONSOLE";
if (!String.IsNullOrEmpty(_fileResourceDownloadUrl) )
{
try
{
DriveService _service = AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, MY_EMAIL_ADD);
var x = _service.HttpClient.GetStringAsync(_fileResourceDownloadUrl);
string arrBytes = x.Result;
System.IO.File.WriteAllText(
@"PATH_TO_SAVE_FILE" + fileNameToSave, arrBytes);
return View("VIEW_TO_RETURN");
}
catch (AggregateException e)
{
return View("ERROR_VIEW");
}
catch (InvalidOperationException e)
{
return View("ERROR_VIEW");
}
}
else
{
return null;
}
}
5. You need to create a client ID in google developer console for installed applications. This will give you Redirect Uri's which don't need to be updated.
Hope this saves someone's time...as it took me a long time to reach here... Please let me know if anyone needs the complete code... Enjoy :)