我必须执行一项非常简单的任务,才能通过Google Api获取Google帐户的所有任务。
在我开始这个问题之前,我阅读文档,尝试搜索答案,阅读技术论坛等
首先,我配置了开发人员控制台: - 打开所需的API:例如Tasks API - 创建并下载到我的项目所有通用键:WEB,SERVICE + P12 KEY,INSTALL APP - 此外,安装在项目中所有需要的Nugets:Auth,Client,Core,Tasks.v1
可能是我错了,但非常关注(常见问题):
1。为什么需要太多的密钥变体? Google IP只是服务,只需要一个令牌(密钥) 对于软件,后端,桌面,IOS / Android / Blackberry上的桌面,服务,......之间没有根本区别。 软件只需从/向某些Google帐户获取/设置数据。 这很混乱。
2。 Google Developers Console - 对于想要获得与Google应用同步功能的有序用户而言,这不是简单明了的工具。 为什么Google没有简单的信任令牌生成帐号?
3。 Google文档并不完整而复杂。空白位置太多:例如REST查询 - 看不到包含测试数据和标题的任何正确和工作示例。 可能有任何已知的 - 它在哪里?
好的,让我们开始练习 - VS2012上的简单桌面应用程序:
4. 大多数谷歌样本说我需要使用GoogleWebAuthorizationBroker 下面的代码与文档中的所有代码相似
client_secret.json - 来自Google Developers Console的JSON文件 我尝试使用密码的所有JSON文件,并手动设置机密 但没有,GoogleWebAuthorizationBroker.AuthorizeAsync试图打开糟糕的浏览器窗口并结束
UserCredential credential;
var stream = new FileStream(@"..." + client_secret.json, FileMode.Open, FileAccess.Read);
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { TasksService.Scope.Tasks },
"user",
CancellationToken.None,
new FileDataStore("Tasks.Auth.Store")).Result;
// Create the service.
var service = new TasksService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Api Project"
});
TaskLists results = service.Tasklists.List().Execute();
什么是错误???
5. Ок,试着用这个:
GoogleAuthorizationCodeFlow flow;
using (var stream = new FileStream(@"..." + client_secret.json, FileMode.Open, FileAccess.Read))
{
flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
DataStore = new FileDataStore("Tasks.Auth.Store"),
ClientSecrets = GoogleClientSecrets.Load(stream).Secrets,
Scopes = new[] { TasksService.Scope.Tasks }
});
}
var result = new AuthorizationCodeWebApp(flow, "", "")
.AuthorizeAsync("user_id", CancellationToken.None).Result;
// The data store contains the user credential, so the user has been already authenticated.
TasksService service = new TasksService(new BaseClientService.Initializer
{
ApplicationName = "API Project",
HttpClientInitializer = result.Credential
});
var lists = service.Tasklists.List().Execute();
始终 - 不是认证。 我尝试更改“user_id” - 但没有 有什么问题???
5. Ок,试着用这个:
string serviceAccountEmail =“xxx@developer.gserviceaccount.com”;
var certificate = new X509Certificate2(@"...\\key.p12", "notasecret", X509KeyStorageFlags.Exportable);
//var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { TasksService.Scope.Tasks }
}.FromCertificate(certificate));
// Create the service.
var service = new TasksService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "API Project",
});
var lists = service.Tasklists.List().Execute();
if (lists.Items != null)
{
foreach (TaskList lst in lists.Items)
{
var title = lst.Title;
var tasks = service.Tasks.List(lst.Id).Execute();
if (tasks.Items != null)
{
foreach (Task tsk in tasks.Items)
{
}
}
}
}
!!!我有权访问,但不能访问我的帐户。 Google只返回一个EMPTY敌人任务列表(不是我的2个列表,我的帐户中包含许多任务)
有什么问题???
P.S。请帮忙。我陷入僵局。我没想到谷歌这么糟糕。 请不要将我重定向到其他论坛或主题。我以前都读过。
由于