因此,我从此处复制了 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);
}
UYOU
鸿蒙传说
相关分类