Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件上载到google drive_C#_.net_Google Drive Api - Fatal编程技术网

C# 将文件上载到google drive

C# 将文件上载到google drive,c#,.net,google-drive-api,C#,.net,Google Drive Api,我正在尝试将文件上载到驱动器中,但响应始终为空 我创建了一个开发者帐户,然后我得到了我的api密钥和我的客户机密码和id 但我仍然认为问题在于身份验证步骤 static string ApplicationName = "WebApi"; private static readonly IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.

我正在尝试将文件上载到驱动器中,但响应始终为空

我创建了一个开发者帐户,然后我得到了我的api密钥和我的客户机密码和id 但我仍然认为问题在于身份验证步骤

   static string ApplicationName = "WebApi";
    private static readonly IAuthorizationCodeFlow flow =
     new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
     {
         ClientSecrets = new ClientSecrets
         {
             ClientId = "my_client_ID",
             ClientSecret = "my_client_secret",

         },
         Scopes = new[] { DriveService.Scope.Drive,
                          DriveService.Scope.DriveAppdata,
                          DriveService.Scope.DriveFile,
                          DriveService.Scope.DriveMetadataReadonly,
                          DriveService.Scope.DriveReadonly,
                          DriveService.Scope.DriveScripts},
         DataStore = new FileDataStore("Drive.Api.Auth.Store")
     });


      public MainWindow()
    {
        InitializeComponent();


        var service = new DriveService(new Google.Apis.Services.BaseClientService.Initializer()
        {
            ApiKey = "my_api_key",
            HttpClientInitializer = flow as UserCredential,
            ApplicationName = ApplicationName
        });

        insertFile(service, "file_name.mdf", "", "", "application/octet-stream", "file_name.mdf");

    }



       private static Google.Apis.Drive.v2.Data.File insertFile(DriveService service, String title, String description, String parentId, String mimeType, String filename)
    {

        // File's metadata
        Google.Apis.Drive.v2.Data.File body = new 
        Google.Apis.Drive.v2.Data.File();
        body.Title = title;
        body.Description = description;
        body.MimeType = mimeType;
        body.Parents = body.Parents = new List<ParentReference>();

        // Set the parent folder.
        if (!String.IsNullOrEmpty(parentId))
        {
            body.Parents = new List<ParentReference>()
      {new ParentReference() {Id = parentId}};
        }

        // File's content.
        byte[] byteArray = System.IO.File.ReadAllBytes(filename);
        MemoryStream stream = new MemoryStream(byteArray);

        try
        {
            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream,mimeType );
            request.Upload();

            Google.Apis.Drive.v2.Data.File file = request.ResponseBody;

            // Uncomment the following line to print the File ID.
            Console.WriteLine("File ID: " + file.Id);

            return file;
        }
        catch (Exception e)
        {    
            throw e;
        }
    }
静态字符串ApplicationName=“WebApi”;
私有静态只读IAuthorizationCodeFlow流=
新的GoogleAuthorizationCodeFlow(新的GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets=新的ClientSecrets
{
ClientId=“我的客户号”,
ClientSecret=“我的客户的秘密”,
},
Scopes=new[]{DriveService.Scope.Drive,
DriveService.Scope.DriveAppdata,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveMetadataReadonly,
DriveService.Scope.DriveReadonly,
DriveService.Scope.DriveScripts},
数据存储=新文件数据存储(“Drive.Api.Auth.Store”)
});
公共主窗口()
{
初始化组件();
var service=newdriveservice(新的Google.api.Services.BaseClientService.Initializer()
{
ApiKey=“我的api\U密钥”,
HttpClientInitializer=作为用户凭证流动,
ApplicationName=ApplicationName
});
insertFile(服务,“file_name.mdf”,“application/octet stream”,“file_name.mdf”);
}
私有静态Google.api.Drive.v2.Data.File插入文件(DriveService服务、字符串标题、字符串描述、字符串parentId、字符串mimeType、字符串文件名)
{
//文件的元数据
Google.api.Drive.v2.Data.File body=新建
Google.api.Drive.v2.Data.File();
正文.标题=标题;
body.Description=描述;
body.MimeType=MimeType;
body.Parents=body.Parents=new List();
//设置父文件夹。
如果(!String.IsNullOrEmpty(parentId))
{
body.Parents=新列表()
{new ParentReference(){Id=parentId}};
}
//文件的内容。
byte[]byteArray=System.IO.File.ReadAllBytes(文件名);
MemoryStream stream=新的MemoryStream(byteArray);
尝试
{
fileresource.InsertMediaUpload请求=service.Files.Insert(body、stream、mimeType);
request.Upload();
Google.api.Drive.v2.Data.File File=request.ResponseBody;
//取消注释以下行以打印文件ID。
Console.WriteLine(“文件ID:+File.ID”);
返回文件;
}
捕获(例外e)
{    
投掷e;
}
}
我正在尝试插入的主窗口中创建服务 插入文件函数中的文件


我收到一个空响应,请帮助。

您是否尝试插入检查程序以查看其实际遇到问题的位置?您是否尝试插入检查程序以查看其实际遇到问题的位置?