gpt4 book ai didi

c# - 在可移植库中使用适用于 .NET 的 Google API 客户端库

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:24 25 4
gpt4 key购买 nike

他们写道:“支持的平台:可移植类库”。但是后来我在可移植类中编写这段代码时,出现错误:

public async void MyFuncrion()
{
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
//new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read),
new ClientSecrets
{
ClientId = "", //"PUT_CLIENT_ID_HERE",
ClientSecret = "" //"PUT_CLIENT_SECRETS_HERE"
},
new[] { TasksService.Scope.Tasks },
"user",
CancellationToken.None);

var service = new TasksService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Tasks API Sample",
});

TaskLists results = await service.Tasklists.List().ExecuteAsync();

foreach (var tasklist in results.Items)
{
TasklistTitlesCollection.Add(tasklist.Title + " " + tasklist.Updated);
// You can get data from the file (using file.Title for example)
// and append it to a TextBox, List, etc.
}
}

此处错误:“UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync()”,他不在可移植库中工作。我如何使用库,在没有 GoogleWebAuthorizationBroker 的情况下获取任务。我自己已经获得了访问 token ,我只需要 TasksService,也许我可以在构造函数 TasksService 中传递我的访问 token 和其他 token ?

References

最佳答案

我用这篇文章来打破这堵墙google .net library

这里我从 PCL 和 windows8 中传递了一些我的代码。

PCL:你需要提供DataStore

private async Task<GoogleAuthorizationCodeFlow.Initializer> InitInitializer()
{
_iDataStore = await _userCredential.GetDataStore(); //new StorageDataStore()
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = ClientId, //"PUT_CLIENT_ID_HERE",
ClientSecret = ClientSecret, //"PUT_CLIENT_SECRET_HERE"
},
Scopes = new[] { TasksService.Scope.Tasks },
DataStore = (Google.Apis.Util.Store.IDataStore)_iDataStore //new StorageDataStore()
};
return initializer;
}

然后

public async Task<TasksService> Prepare()
{
GoogleAuthorizationCodeFlow.Initializer initializer = await InitInitializer();

Object credential = new Object();

if (String.IsNullOrEmpty(_username))
{
return null;
}

TasksService service = null;
try
{
credential = await _userCredential.GetCredential(initializer, _username);
}
catch (Google.Apis.Auth.OAuth2.Responses.TokenResponseException e)
{
service = null;
return service;
}
catch
{
return null;
}
service = new TasksService(new BaseClientService.Initializer
{
HttpClientInitializer = (UserCredential)credential,
ApplicationName = _applicationName,
});
return service;
}

1) 在 Windows store 中,您需要提供 StorageDataStore 并将其遍历到 pcl。2)使用

AuthorizationCodeWinRTInstalledApp(initializer).AuthorizeAsync(username, new CancellationToken(false))

从谷歌图书馆 (Google.Apis.Auth.OAuth2) 获取您的凭证并将其遍历到 pcl

关于c# - 在可移植库中使用适用于 .NET 的 Google API 客户端库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22943694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com