WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: mcvpjd3 on September 12, 2014, 04:06:39 AM

Title: Google Drive question
Post by: mcvpjd3 on September 12, 2014, 04:06:39 AM
Just a quick question that popped into my head. I haven't looked into this and not even planning to use it, but just curious about it.

Can winbatch be used to update a document in Google drive? (for example put data into a Google sheet)?

Thanks
Title: Re: Google Drive question
Post by: stanl on September 12, 2014, 06:03:16 AM
Google provides C# code (see below) to access the drive. Doubt this can be converted to WB, and it would take more of a C# expert to determine if the WB CLR could work with it


using System;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Services;
namespace GoogleDriveSamples
{
    Class DriveCommandLineSample
    {
        static void Main(string[] args)
        {
            String CLIENT_ID = "YOUR_CLIENT_ID";
            String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
            // Register the authenticator and create the service
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
            var service = new DriveService(new BaseClientService.Initializer()
            {
                Authenticator = auth
            });
            File body = new File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "text/plain";
            byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
            request.Upload();
            File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();
        }
        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);
            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();
            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }
    }
}
 
Title: Re: Google Drive question
Post by: td on September 12, 2014, 02:26:32 PM
Google provides a dotNet API for Google Sheet, as well. Since I have never used it nor spent much time even looking it, I can't say how much of it could be used with WinBatch CLR hosting.  Guess would be that it would work as long as you don't do anything that requires delegates for event handling.