我一直在玩.NET Google Drive API,我可以通過API成功連接,驗證並獲取文件列表(我們的Google雲端硬盤中已經有數千個文件帳戶,通過網絡直接上傳)。通過API上傳到Google雲端硬盤的「禁止」錯誤
但是,在嘗試寫入數據時遇到了兩個非常類似的錯誤:如果我嘗試上載測試文件(test.txt
),則出現403「禁止」錯誤。要創建一個新的文件夾給我一個類似的錯誤:
Exception thrown: 'Google.GoogleApiException' in Google.Apis.dll
Additional information: Google.Apis.Requests.RequestError
Insufficient Permission [403] Location[ - ] Reason[insufficientPermissions] Domain[global]
我已經遵循「快速入門」教程,以及其他類似的問題在這裏,但我看不出還有什麼我必須做的。這是我上傳文件的示例代碼;我需要在代碼或Google雲端硬盤帳戶本身中添加/更改哪些內容以允許上傳文件和創建文件夾?
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
@DaImTo:代碼已經在那裏了? – KenD
哎呀對不起滾動條:/ – DaImTo