无法上传到 Google 驱动器
Cannot Upload to Google Drive
我正在尝试将 JSON 文件上传到任何人的 GDrive 中的根文件夹。我在 Google 中搜索了所有答案,同时尝试自己修复它。无论我做了什么 request.Responsebody 总是 returns 无效。在这里我尝试做一个空白的 .txt 文件(仍然不起作用)。
这是获取凭证的代码,然后它将生成的 DeviceService 设置为变量。
//GDrive Cred
public static void GetCred()
{
//Get Credential
UserCredential credential;
using (var stream =
new FileStream(Path + "\client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = System.IO.Path.Combine(credPath, ".credentials/drive-dotnet-quickstart");
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,
});
mService = service;
}
这是上传文件的代码。
//Save JSON to GDrive
public static void SaveJSONtoDrive()
{
GetCred();
// File's metadata.
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = "ATGEntries.txt";
body.Description = "Database JSON for Adventurer's Tour Guide";
//body.MimeType = "application/json";
body.MimeType = "text/plain";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = "root" } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(Path + "\ATGEntries.txt");
MemoryStream stream = new MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = mService.Files.Insert(body, stream, "text/plain");
request.UploadAsync();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
// Uncomment the following line to print the File ID.
Console.WriteLine("File ID: " + file.Id);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
有人可以帮我解决这个奇怪的错误吗?
我不敢相信这不是任何地方,所以我会回答我自己的问题,希望它能帮助其他人。
注意设置 'credPath' 变量的行
credPath = System.IO.Path.Combine(credPath, ".credentials/drive-dotnet-quickstart");
确保“.credentials/”之后的部分名称必须与您在 Google 开发者控制台中为 OAuth 2.0 客户端 ID 命名的名称相同。这应该可以解决问题。
我正在尝试将 JSON 文件上传到任何人的 GDrive 中的根文件夹。我在 Google 中搜索了所有答案,同时尝试自己修复它。无论我做了什么 request.Responsebody 总是 returns 无效。在这里我尝试做一个空白的 .txt 文件(仍然不起作用)。
这是获取凭证的代码,然后它将生成的 DeviceService 设置为变量。
//GDrive Cred
public static void GetCred()
{
//Get Credential
UserCredential credential;
using (var stream =
new FileStream(Path + "\client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = System.IO.Path.Combine(credPath, ".credentials/drive-dotnet-quickstart");
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,
});
mService = service;
}
这是上传文件的代码。
//Save JSON to GDrive
public static void SaveJSONtoDrive()
{
GetCred();
// File's metadata.
Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
body.Title = "ATGEntries.txt";
body.Description = "Database JSON for Adventurer's Tour Guide";
//body.MimeType = "application/json";
body.MimeType = "text/plain";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = "root" } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(Path + "\ATGEntries.txt");
MemoryStream stream = new MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = mService.Files.Insert(body, stream, "text/plain");
request.UploadAsync();
Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
// Uncomment the following line to print the File ID.
Console.WriteLine("File ID: " + file.Id);
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}
}
有人可以帮我解决这个奇怪的错误吗?
我不敢相信这不是任何地方,所以我会回答我自己的问题,希望它能帮助其他人。
注意设置 'credPath' 变量的行
credPath = System.IO.Path.Combine(credPath, ".credentials/drive-dotnet-quickstart");
确保“.credentials/”之后的部分名称必须与您在 Google 开发者控制台中为 OAuth 2.0 客户端 ID 命名的名称相同。这应该可以解决问题。