close

主要開發目的 需要使用GOOGLE 雲端

Google OAuth 2.0 開放授權

http://vito-note.blogspot.com/2015/04/google-oauth-20.html

參考網頁
https://www.daimto.com/google-drive-api-c/

using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

////////////////////////////////////////////////////////////

 UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                 new ClientSecrets { ClientId = "GOOGLE帳號申請憑證ClientId ", ClientSecret = "GOOGLE帳號申請憑證ClientSecret " },
                 new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile },
                 "test",
                 CancellationToken.None,
                 new FileDataStore("Drive.Auth.Store")).Result;

            _service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = "test",
            });
            //Download("https://drive.google.com/file/d/1CkrPC7NXtprDMGRsdHTChmZJcxARcK32/view?usp=sharing", _service);
             Google.Apis.Drive.v3.Data.File body = updateFile(_service, @"本地端上傳檔案位置1512458432647.jpg", "帳號內部的ID,這裡指GOOGLE裡面所有檔案GOOGLE都會有給一個ID");

            //拿取目前帳號內部的所有文件資料,可以下中斷點看內容
            try
            {
                FilesResource.ListRequest request = _service.Files.List();
                FileList files = request.Execute();
            }
            catch (Exception e)
            {

                throw;

public static Google.Apis.Drive.v3.Data.File updateFile(DriveService _service, string _uploadFile, string _fileId)
        {

            if (System.IO.File.Exists(_uploadFile))//檢查檔案是否有拿到
            {
                Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();//把檔案填入GOOGLE V3的格式
                body.Name = System.IO.Path.GetFileName(_uploadFile);
                body.Description = "File updated by Diamto Drive Sample";
                body.MimeType = GetMimeType(_uploadFile);//需要去計算檔案的大小

                byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    //FilesResource.UpdateMediaUpload request = _service.Files.Update(body, _fileId, stream, GetMimeType(_uploadFile));
                    //request.Upload();
                    FilesResource.CreateMediaUpload  request = _service.Files.Create(body, stream, "image/jpeg");
                    request.Fields = "1bhS-cBJ5btpC2ixwxt3qoAjoFyelwQiJ";
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: " + e.Message);
                    return null;
                }
            }
            else
            {
                Console.WriteLine("File does not exist: " + _uploadFile);
                return null;
            }

        }

        private static string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 萌新-工程師 的頭像
    萌新-工程師

    工程師的雜記

    萌新-工程師 發表在 痞客邦 留言(0) 人氣()