这是我第一次在C#中使用API。 我将使用API上传我从网络摄像头录制的视频。
这就是我所做的:
我不知道下一步。 我读过Performing Simple Upload,但不知道将代码放在哪里。
我创建了一个函数upload(),但是var,新File(),名称和driveService出现错误-有关System.IO.File的信息,但是我找不到具有该名称的程序包,而driveService没有存在
我遵循了40049021和1228046之类的其他教程,但我不断遇到错误,他们没有使用v3上推荐的内容。
这是我的Program.cs:
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;
using System.Windows.Forms;
namespace AForgeAccord_SavePicVid
{
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("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.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();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
编辑: 这是Google提供的“基本上传”代码:
var fileMetadata = new File()
{
Name = "photo.jpg"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("files/photo.jpg",
System.IO.FileMode.Open))
{
request = driveService.Files.Create(
fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
请帮助我了解如何创建上传文件(.jpg和.avi)的功能
EDIT2: 这就是我用来测试的代码
static void InitializeGAPI()
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.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();
string uploadfile = @"C:\Users\iya\Desktop\tmp\Image-2019-01-31_10-25-32-AM.jpg";
if (System.IO.File.Exists(uploadfile))
{
MessageBox.Show("Uploading..." + uploadfile);
var fileMetadata = new Google.Apis.Drive.v3.Data.File();
fileMetadata.Name = System.IO.Path.GetFileName(uploadfile);
fileMetadata.MimeType = GetMimeType(uploadfile);
//fileMetadata.Parents = new List<string> { "Test Folder" };
FilesResource.CreateMediaUpload request;
try
{
using (var stream = new System.IO.FileStream(uploadfile, System.IO.FileMode.Open))
{
request = service.Files.Create(fileMetadata, stream, fileMetadata.MimeType);
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
var fili = file.Id;
Console.WriteLine("File ID: " + file.Id);
}
catch (Exception e)
{
MessageBox.Show("An error occurred: " + e.Message);
}
}
else
{
MessageBox.Show("File does not exist: " + uploadfile);
}
}
我的ApplicationName应该是什么?