我的Windows服务中的Google Drive API V3

问题描述:

我在我的Windows服务中使用Google Drive API V3,这在我的本地系统中工作正常,但是当我要部署它时,API身份验证过程无法正常工作。我没有从API获得任何回应。我的Windows服务中的Google Drive API V3

下面是我的代码

string[] scopes = new string[] { DriveService.Scope.Drive, 
DriveService.Scope.DriveFile, DriveService.Scope.DriveAppdata }; 


     credPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData), ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name); 


     var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets 
     { 
      ClientId = clientId, 
      ClientSecret = clientSecret 
     }, 
     scopes, 
     "saurabh.katiyar", 
     CancellationToken.None, 
     new FileDataStore(credPath, true)).Result; 


     return new DriveService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = applicationName 
     }); 
+0

变种凭证= GoogleWebAuthorizationBroker.AuthorizeAsync(新ClientSecrets { 客户端Id =的clientId, ClientSecret = clientSecret }, 范围, Environment.UserName, CancellationToken.None, 新FileDataStore(credPath,真))。结果; –

尝试使用示例代码.net文档中:

从给定的样本进行比较你的代码。

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.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(); 

     } 
    } 
} 

要进一步阅读,你可以检查Authentication and authorization