36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
|
|
using Google.Apis.Auth.OAuth2;
|
|||
|
|
using Google.Apis.Drive.v3;
|
|||
|
|
using Google.Apis.Services;
|
|||
|
|
|
|||
|
|
namespace WebAPI
|
|||
|
|
{
|
|||
|
|
public class GoogleDriveHelper
|
|||
|
|
{
|
|||
|
|
public DriveService Service { get; set; }
|
|||
|
|
const string APPLICATION_NAME = "Diuna";
|
|||
|
|
static readonly string[] Scopes = { DriveService.Scope.Drive };
|
|||
|
|
public GoogleDriveHelper()
|
|||
|
|
{
|
|||
|
|
InitializeService();
|
|||
|
|
}
|
|||
|
|
private void InitializeService()
|
|||
|
|
{
|
|||
|
|
var credential = GetCredentialsFromFile();
|
|||
|
|
Service = new DriveService(new BaseClientService.Initializer()
|
|||
|
|
{
|
|||
|
|
HttpClientInitializer = credential,
|
|||
|
|
ApplicationName = APPLICATION_NAME
|
|||
|
|
});
|
|||
|
|
}
|
|||
|
|
private GoogleCredential GetCredentialsFromFile()
|
|||
|
|
{
|
|||
|
|
GoogleCredential credential;
|
|||
|
|
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
|
|||
|
|
{
|
|||
|
|
credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
|
|||
|
|
}
|
|||
|
|
return credential;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|