 |
|
C# WinApp อยากจะลองเขียนโปรแกรมที่ใช้งาน google drive ดูครับต้องเริ่มยังไงบ้าง |
|
 |
|
|
 |
 |
|
ดูแล้วง่ายๆ เลยครับ
Code (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;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DriveQuickstart
{
class Program
{
// If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-dotnet-quickstart.json
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.Read();
}
}
}
|
 |
 |
 |
 |
Date :
2017-01-12 13:43:29 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (VB.NET)
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v3
Imports Google.Apis.Drive.v3.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks
Namespace DriveQuickstart
Class Program
' If modifying these scopes, delete your previously saved credentials
' at ~/.credentials/drive-dotnet-quickstart.json
Shared Scopes As String() = {DriveService.Scope.DriveReadonly}
Shared ApplicationName As String = "Drive API .NET Quickstart"
Private Shared Sub Main(args As String())
Dim credential As UserCredential
Using stream = New FileStream("client_secret.json", FileMode.Open, FileAccess.Read)
Dim credPath As String = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json")
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, New FileDataStore(credPath, True)).Result
Console.WriteLine(Convert.ToString("Credential file saved to: ") & credPath)
End Using
' Create Drive API service.
Dim service = New DriveService(New BaseClientService.Initializer() With { _
Key .HttpClientInitializer = credential, _
Key .ApplicationName = ApplicationName _
})
' Define parameters of request.
Dim listRequest As FilesResource.ListRequest = service.Files.List()
listRequest.PageSize = 10
listRequest.Fields = "nextPageToken, files(id, name)"
' List files.
Dim files As IList(Of Google.Apis.Drive.v3.Data.File) = listRequest.Execute().Files
Console.WriteLine("Files:")
If files IsNot Nothing AndAlso files.Count > 0 Then
For Each file As var In files
Console.WriteLine("{0} ({1})", file.Name, file.Id)
Next
Else
Console.WriteLine("No files found.")
End If
Console.Read()
End Sub
End Class
End Namespace
|
 |
 |
 |
 |
Date :
2017-01-12 13:44:20 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ก็ลงโปรแ กรมของ google drive ก็ได้น่ะครับ
มันจะให้ตั้ง folder หลักของโปรแกรม
แล้วให้ user เอาไฟล์ไปไว้ในนั้น
มันก็จะ sync ข้อมูลขึ้นไปเอง
|
 |
 |
 |
 |
Date :
2017-01-12 14:28:33 |
By :
fonfire |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2017-01-12 15:02:35 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|