【问题标题】:Google API file permissions?谷歌 API 文件权限?
【发布时间】:2017-07-31 01:25:50
【问题描述】:

我正在尝试编译以下代码,完全基于在 Google Drive API herehere 上找到的代码。系统提示我在浏览器中允许权限,但随后出现异常:

Google.Apis.dll 中出现“Google.GoogleApiException”类型的未处理异常

附加信息:Google.Apis.Requests.RequestError 权限不足 [403]

代码:

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.ReadWrite))
      {
        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,
      });

      var fileMetadata = new Google.Apis.Drive.v3.Data.File()
      {
        Name = "Invoices",
        MimeType = "application/vnd.google-apps.folder"
      };

      var request = service.Files.Create(fileMetadata);
      request.Fields = "id";
      var file = request.Execute();
      Console.WriteLine("Folder ID: " + file.Id);

      Console.Read();
    }
  }
}

我在这里缺少什么?谢谢。

【问题讨论】:

    标签: .net google-api google-drive-api


    【解决方案1】:

    疏忽,抱歉。我把DriveService.Scope.DriveReadonly改成了DriveService.Scope.DriveFile

    进行上述更改后,我必须导航到C:\users\UserName\Documents\.credentials\drive-dotnet-quickstart.json 文件夹,删除那里的文件:Google.Apis.Auth.OAuth2.Responses.TokenResponse-user。它会在重新执行时重新创建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      相关资源
      最近更新 更多