谷歌OAuth2样本不工作
本文关键字:工作 样本 OAuth2 谷歌 | 更新日期: 2023-09-27 18:05:53
我试图学习如何使用谷歌API,我已经开始了一个例子,可以在这里找到:https://developers.google.com/drive/web/quickstart/dotnet
我相信我完全按照说明做了,我复制粘贴了所有的代码,但是我不能让它工作。
如果我调试它Visual Studio问我的位置GoogleClientSecrets.cs当我到达GoogleWebAuthorizationBroker。AuthorizeAsync方法。
如果我只是继续代码抛出HttpListenerException与消息(翻译自瑞典语)"提供的网络名称格式错误"。错误码为1214
我猜它应该打开浏览器在这一点上,但它没有,这可能是问题?
这对我来说都是新的,所以我很难找到解决办法。我在谷歌上搜索解决方案已经有一段时间了。
任何建议都将非常感谢。
这是完整的代码:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.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
{
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
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,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.MaxResults = 10;
// List files.
IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute()
.Items;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Title, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.Read();
}
}
}
好吧,我没有答案,为什么这是不工作,但我找到了另一种方式:跳过驱动器API,去GData API。
我可以在这里找到一个完美无瑕的样本:https://developers.google.com/google-apps/spreadsheets/?hl=en