亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

C#
牧羊人nacy 2022-11-22 16:05:42
因此,我從此處復制了 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);            }
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

我剛剛測試了以下代碼,它工作正常。我希望它能幫助你。


class Program

    {

        static void Main(string[] args)

        {


            var service = Oauth2Example.GetDriveService(@"C:\Users\linda\Documents\.credentials\NativeClient.json", "test",

                new[] {Google.Apis.Drive.v3.DriveService.Scope.Drive});


            var fileMetadata = new Google.Apis.Drive.v3.Data.File()

            {

                Name = "flag.jpg"

            };


            FilesResource.CreateMediaUpload request;

            using (var stream = new System.IO.FileStream(@"C:\temp\flag.jpg", System.IO.FileMode.Open))

            {

                request = service.Files.Create(fileMetadata, stream, "image/jpeg");

                request.Fields = "id";

                request.Upload();

            }


            var file = request.ResponseBody;

            Console.WriteLine("File ID: " + file.Id);

            Console.ReadKey();

        }

    }

這是Oauth2Example的鏈接


查看完整回答
反對 回復 2022-11-22
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

其他遇到云端硬盤上傳問題的人應該查看您上傳的異常,這將使您更好地了解實際問題。


示例代碼:


var progress = request.Upload();


if (progress.Exception != null)

{

   //Log execption, or break here to debug

   YourLoggingProvider.Log(progress.Exception.Message.ToString());

}

這就是我找到問題根源的方式。


查看完整回答
反對 回復 2022-11-22
  • 2 回答
  • 0 關注
  • 195 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號