2017-03-09 94 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。它將在重新執行時重新創建。