我试图编译以下代码,完全基于Google 这里和这里上的代码。提示我允许在浏览器中使用权限,但随后得到了一个异常,该异常的内容如下:
Google.GoogleApiException类型的未处理异常发生在Google.Apis.dll中
附加信息: 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();
}
}
}
我在这里错过了什么?谢谢。
发布于 2017-03-09 13:56:54
监督,对不起。我把DriveService.Scope.DriveReadonly
改成了DriveService.Scope.DriveFile
在上面的更改之后,我不得不导航到C:\users\UserName\Documents\.credentials\drive-dotnet-quickstart.json
文件夹,删除那里的文件:Google.Apis.Auth.OAuth2.Responses.TokenResponse-user
。它将在重新执行时重新创建。
https://stackoverflow.com/questions/42705871
复制相似问题