2017-06-27 69 views
0

因此,我在Google Drive API Rest C#中使用此Google´s step-by-step。我基本上想從我的控制檯C#應用程序中將我的文件列入我的Google Drive。C#與谷歌驅動器API:{「訪問被拒絕」}

我創建了Oauth2憑證,下載了json文件,導入到我的Visual Studio項目中,使用NuGet管理器安裝Google Drive APIs v3,複製並粘貼了C#代碼(如下)。但在我的GoogleWebAuthorizationBroker.AuthorizeAsync()方法中,我不斷收到錯誤{「Access is denied」}(在innerException中)。誰能幫幫我嗎?

我的C#代碼:

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 Drive_API_Quickstart__2nd_attempt_ 
{ 
    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.Read)) 
      { 
       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, 
      }); 

      // Define parameters of request. 
      FilesResource.ListRequest listRequest = service.Files.List(); 
      listRequest.PageSize = 10; 
      listRequest.Fields = "nextPageToken, files(id, name)"; 

      // List files. 
      IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute() 
       .Files; 
      Console.WriteLine("Files:"); 
      if (files != null && files.Count > 0) 
      { 
       foreach (var file in files) 
       { 
        Console.WriteLine("{0} ({1})", file.Name, file.Id); 
       } 
      } 
      else 
      { 
       Console.WriteLine("No files found."); 
      } 
      Console.Read(); 

     } 
    } 
} 

調試錯誤:

System.AggregateException was unhandled 
    HResult=-2146233088 
    Message=One or more errors occurred. 
    Source=mscorlib 
    StackTrace: 
     at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) 
     at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) 
     at System.Threading.Tasks.Task`1.get_Result() 
     at Drive_API_Quickstart__2nd_attempt_.Program.Main(String[] args) in W:\Adriano\Visual Studio\Google Drive\Drive API Quickstart (2nd attempt)\Drive API Quickstart (2nd attempt)\Program.cs:line 34 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
     ErrorCode=5 
     HResult=-2147467259 
     Message=Access is denied 
     NativeErrorCode=5 
     Source=System 
     StackTrace: 
      at System.Net.HttpListener.AddAllPrefixes() 
      at System.Net.HttpListener.Start() 
      at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() 
      at Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.<ReceiveCodeAsync>d__8.MoveNext() 
     --- End of stack trace from previous location where exception was thrown --- 
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
      at Google.Apis.Auth.OAuth2.AuthorizationCodeInstalledApp.<AuthorizeAsync>d__8.MoveNext() 
     --- End of stack trace from previous location where exception was thrown --- 
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
      at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__4.MoveNext() 
     --- End of stack trace from previous location where exception was thrown --- 
      at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
      at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
      at Google.Apis.Auth.OAuth2.GoogleWebAuthorizationBroker.<AuthorizeAsync>d__1.MoveNext() 
     InnerException: 

回答

1

你有正確試過這種實現授權?

你提到的那種document quickstart有關於身份驗證的一些注意事項:

Notes

Authorization information is stored on the file system, so subsequent executions will not prompt for authorization. The authorization flow in this example is designed for a command-line application. For information on how to perform authorization in a web application, see the web applications section of the library's OAuth 2.0 guide.

此外,請遵循這是什麼document about OAuth步驟:

To find the redirect URIs for your OAuth 2.0 credentials, do the following:

1.) Open the Credentials page in the API Console.

2.) If you haven't done so already, create your OAuth 2.0 credentials by clicking Create credentials > OAuth client ID.

3.) After you create your credentials, view or edit the redirect URLs by clicking the client ID (for a web application) in the OAuth 2.0 client IDs section.

您可能忽略這些步驟。您也可能有client_secrets.json的文件不正確。我可以問一下,你是如何真正下載文件的?

0

我面臨與Windows桌面程序相同的問題。當我從調試器發送它時,它不起作用。然後我通過使用「以管理員身份運行」選項來解決問題。它彈出與谷歌帳戶選擇窗口。 這是我的代碼。確保您正確選擇了授權密鑰。

void GoogleThread() 
    { 
     try 
     { 
      //Scopes for use with the Google Drive API 
      string[] scopes = new string[] { 
          DriveService.Scope.DriveFile}; 
      var clientId = "Create auth0 Id and copy here";  // From https://console.developers.google.com 
      var clientSecret = "";   // From https://console.developers.google.com 

      // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData% 
      GoogleWebAuthorizationBroker.Folder = "Drive.Sample"; 
      var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets 
      { 
       ClientId = clientId, 
       ClientSecret = clientSecret 
      }, 

      scopes, 
      "Admin", 
      CancellationToken.None, 
      new FileDataStore("Daimto.GoogleDrive.Auth.Store")).Result; 

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

      uploadFile(service, "d:\\bmjo\\EastManTemp.xml", "0B1HV_LVO8x0zSWFrRkE2LVFUQ2c"); 

     } 
     catch (Exception exp) 
     { 
      MessageBox.Show(exp.Message, "Error"); 
     } 
    } 
    // tries to figure out the mime type of the file. 
    private static string GetMimeType(string fileName) 
    { 
     string mimeType = "application/unknown"; 
     string ext = System.IO.Path.GetExtension(fileName).ToLower(); 
     Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 
     if (regKey != null && regKey.GetValue("Content Type") != null) 
      mimeType = regKey.GetValue("Content Type").ToString(); 
     return mimeType; 
    } 
    public static File uploadFile(DriveService _service, string _uploadFile, string _parent) 
    { 

     if (System.IO.File.Exists(_uploadFile)) 
     { 
      File body = new File(); 

      body.Title = System.IO.Path.GetFileName(_uploadFile); 
      body.Description = "File uploaded by Diamto Drive Sample"; 
      body.MimeType = GetMimeType(_uploadFile); 
      body.Parents = new List<ParentReference>();// { new ParentReference() { Id = _parent } }; 
      body.Parents.Add(new ParentReference() { Id = _parent }); 
      // File's content. 
      byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile); 
      System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); 
      try 
      { 
       FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile)); 
       request.Upload(); 
       return request.ResponseBody; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("An error occurred: " + e.Message); 
       return null; 
      } 
     } 
     else 
     { 
      Console.WriteLine("File does not exist: " + _uploadFile); 
      return null; 
     } 

    } 
    private void button1_Click(object sender, EventArgs e) 
    { 
     Thread thread = new Thread(new ThreadStart(GoogleThread)); 
     thread.Start(); 

    }