2017-03-09 97 views
0

我试图编译以下代码,完全基于Google Drive API herehere上的代码。我被提示您允许在浏览器中,但后来有一个例外,上面写着:Google API文件权限?

类型的未处理的异常“Google.GoogleApiException”发生在Google.Apis.dll

其他信息:谷歌.Apis.Requests.RequestError 没有足够的权限[403]

的代码:

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; 


namespace DriveQuickstart 
{ 
    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("client_secret.json", FileMode.Open, FileAccess.ReadWrite)) 
     { 
     string credPath = System.Environment.GetFolderPath(
     System.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)).Result; 
     Console.WriteLine("Credential file saved to: " + credPath); 
     } 

     // Create Drive API service. 
     var service = new DriveService(new BaseClientService.Initializer() 
     { 
     HttpClientInitializer = credential, 
     ApplicationName = ApplicationName, 
     }); 

     var fileMetadata = new Google.Apis.Drive.v3.Data.File() 
     { 
     Name = "Invoices", 
     MimeType = "application/vnd.google-apps.folder" 
     }; 

     var request = service.Files.Create(fileMetadata); 
     request.Fields = "id"; 
     var file = request.Execute(); 
     Console.WriteLine("Folder ID: " + file.Id); 

     Console.Read(); 
    } 
    } 
} 

我在这里错过了什么?谢谢。

回答

0

监督,对不起。我将DriveService.Scope.DriveReadonly更改为DriveService.Scope.DriveFile

上面的更改后,我不得不导航到C:\users\UserName\Documents\.credentials\drive-dotnet-quickstart.json文件夹,删除那里的文件:Google.Apis.Auth.OAuth2.Responses.TokenResponse-user。它将在重新执行时重新创建。