如何让 Google Drive API v3 示例在 C# 中工作?

因此,我从此处复制了 Google Drive API .NET Quickstart (v3)中的代码,代码运行良好,按预期列出了我的 Google Drive 上的所有文件。但是,当我尝试使用此处找到的上传示例代码时,我无法将本地存储的图像上传到我的云端硬盘并request.ResponseBody返回null。我还添加了额外的驱动器范围,但没有帮助。为什么上传文件的示例代码不起作用?

我的代码如下:

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.Threading;

using System.Windows;


namespace Google_Drive_REST_App

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        static string[] Scopes = { DriveService.Scope.Drive,

                       DriveService.Scope.DriveAppdata,

                       DriveService.Scope.DriveFile,

                       DriveService.Scope.DriveMetadataReadonly,

                       DriveService.Scope.DriveReadonly,

                       DriveService.Scope.DriveScripts};

        static string ApplicationName = "Google Drive REST App";


        UserCredential credential;

        DriveService service;


        public MainWindow()

        {

            InitializeComponent();


            using (var stream =

                new System.IO.FileStream("credentials.json", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite))

            {

                // The file token.json stores the user's access and refresh tokens, and is created

                // automatically when the authorization flow completes for the first time.

                string credPath = "token.json";

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(

                    GoogleClientSecrets.Load(stream).Secrets,

                    Scopes,

                    "user",

                    CancellationToken.None,

                    new FileDataStore(credPath, true)).Result;

                Console.WriteLine("Credential file saved to: " + credPath);

            }


牧羊人nacy
浏览 101回答 2
2回答

UYOU

我刚刚测试了以下代码,它工作正常。我希望它能帮助你。class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var service = Oauth2Example.GetDriveService(@"C:\Users\linda\Documents\.credentials\NativeClient.json", "test",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new[] {Google.Apis.Drive.v3.DriveService.Scope.Drive});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var fileMetadata = new Google.Apis.Drive.v3.Data.File()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name = "flag.jpg"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FilesResource.CreateMediaUpload request;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (var stream = new System.IO.FileStream(@"C:\temp\flag.jpg", System.IO.FileMode.Open))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request = service.Files.Create(fileMetadata, stream, "image/jpeg");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.Fields = "id";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.Upload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var file = request.ResponseBody;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("File ID: " + file.Id);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }这是Oauth2Example的链接

鸿蒙传说

其他遇到云端硬盘上传问题的人应该查看您上传的异常,这将使您更好地了解实际问题。示例代码:var progress = request.Upload();if (progress.Exception != null){&nbsp; &nbsp;//Log execption, or break here to debug&nbsp; &nbsp;YourLoggingProvider.Log(progress.Exception.Message.ToString());}这就是我找到问题根源的方式。
打开App,查看更多内容
随时随地看视频慕课网APP