【发布时间】:2016-04-22 21:13:58
【问题描述】:
我一直在玩.NET Google Drive API,我可以通过 API 成功连接、验证并获取文件列表(我们的 Google Drive 帐户中已经有数千个文件,直接通过网络上传) .
但是,我在尝试写入数据时遇到了两个非常相似的错误:如果我尝试上传测试文件 (test.txt),我会收到 403“禁止”错误。尝试创建一个新文件夹会给我一个类似的错误:
抛出异常:Google.Apis.dll 中的“Google.GoogleApiException”
附加信息:Google.Apis.Requests.RequestError
权限不足 [403] 位置[-] 原因[insufficientPermissions] 域[全局]
我已经按照“快速入门”教程以及此处的其他类似问题进行了操作,但我看不出我还需要做什么。这是我上传文件的示例代码;我需要在我的代码或 Google Drive 帐户本身中添加/更改什么,以允许上传文件和创建文件夹?
class GDriveTest
{
static string[] Scopes = { DriveService.Scope.Drive,DriveService.Scope.DriveFile };
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");
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,
});
UploadSingleFile(service);
}
private static void UploadSingleFile(DriveService service)
{
File body = new File();
body.Title = "My document";
body.Description = "A test document";
body.MimeType = "text/plain";
byte[] byteArray = System.IO.File.ReadAllBytes("test.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();
File file = request.ResponseBody;
Console.WriteLine("File id: " + file.Id);
Console.ReadLine();
}
}
【问题讨论】:
-
请添加 UploadSingleFile 的代码
-
@DaImTo: 代码已经存在了吗?
-
哎呀抱歉滚动条:/
标签: c# google-drive-api