我的应用程序有以下问题:我想上传简单的文件到谷歌驱动器,没有错误,但它似乎不工作,因为没有文件被上传。我在谷歌启用了API,补充道
using Google.Apis.Drive.v3.Data;
using Google.Apis.Upload;我的项目,但我不能说出为什么它不能工作。我尝试过谷歌提供的不同解决方案,但都没有奏效。为了获得所需的信息,我使用
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Net;
// 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;
WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = Environment.GetFolderPath(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), new LocalServerCodeReceiver()).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
// new Google.Apis.Drive.v2.DriveService()
var service = new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
Google.Apis.Drive.v3.FilesResource.ListRequest listRequest = service.Files.List();
// listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
//Upload File
gUpload(service, @"C:\Users\xxx\test.txt"); //THIS DOES NOT WORK
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (Google.Apis.Drive.v3.Data.File file in files)
{ //THIS WORKS FINE
Console.WriteLine("{0} ({1})", file.OriginalFilename, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.ReadLine();
}这将为我提供正确的凭据,并列出包含在我的gDrive文件夹中的所有文件以及它的垃圾文件。
另一种方法是,添加文件而不是列表它们无论如何都不起作用:
public static void TestUpload(string filePath, DriveService driveService)
{
if (System.IO.File.Exists(filePath.ToString()))
{
File body = new File();
body.Name = System.IO.Path.GetFileName(filePath.ToString());
body.Description = "Test Description";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.CreateMediaUpload request = driveService.Files.Create(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody; //returns null value
}
}这运行时没有任何错误,但是请求为null,没有上传任何内容,我的其他尝试之一也运行没有错误,但是没有上传任何内容。
public static bool gUpload(Google.Apis.Drive.v3.DriveService service, string filePath)
{
string fileID = "0B-W1aRTTOB1QM3hPd0dOUFVObHM"; //random
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open);
File fBody = new File { Name = "test.txt" };
var r = service.Files.Update(fBody, fileID, stream, "application/octet-stream");
r.ChunkSize = ResumableUpload.MinimumChunkSize;
r.Resume();
// UploadStatus.Completed
return true;
}我希望有谁在那里给我一个工作的答案,如何改变代码,使上传工作。
向杰克问好
发布于 2017-09-04 08:59:17
看起来您使用了错误的作用域,应该是DriveService.Scope.Drive (但可能是过量的)。此外,当您更新作用域列表时,不要忘记从缓存的凭据中删除。您可以检查作用域这里。
https://stackoverflow.com/questions/45921088
复制相似问题