【问题标题】:Using Google APIs Client Library for .NET in portable library在可移植库中使用适用于 .NET 的 Google API 客户端库
【发布时间】:2014-04-08 17:04:28
【问题描述】:

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

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 的情况下获得任务。我自己已经获得了访问令牌,我只需要TasksService,也许我可以在构造函数TasksService 中传递我的访问令牌和其他?

【问题讨论】:

  • 错误信息是什么?您是否通过 NuGet 安装了 Google API?
  • 当前上下文中不存在名称“GoogleWebAuthorizationBroker”。
  • 还有其他问题。我无法将我的 StorageDataStore 实现提供给 GoogleWebAuthorizationBroker。 GoogleWebAuthorizationBroker 没有这个参数。 developers.google.com/api-client-library/dotnet/guide/aaa_oauth
  • 你是通过 NuGet 安装的吗?你有没有引用命名空间Google.Apis.Auth.OAuth2

标签: c# windows-phone-8 portable-class-library google-api-dotnet-client google-tasks-api


【解决方案1】:

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

这里我过去了一些来自 PCL 和 windows8 的代码。

PCL: 您需要提供数据存储

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 商店中,您需要提供 StorageDataStore 并将其遍历到 pcl。 2)使用

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

从谷歌库 (Google.Apis.Auth.OAuth2) 获取您的凭据并将其遍历到 pcl

【讨论】:

    【解决方案2】:

    你有两个选择:

    1.将 foo 声明为异步方法:

     async void Foo()
    

    2.删除等待,并获取任务的结果,因此您的代码应如下所示:

     serCredential credential = 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).Result;
    

    【讨论】:

    • 我忘记在我的代码中添加这个 public async void MyFuncrion() 了。
    • 解决方案是什么?我也有同样的问题。
    • @Jesse 你需要像这里一样提供你自己的 StorageDataStore:developers.google.com/api-client-library/dotnet/guide/aaa_oauth 并在便携式和存储应用程序中分离一些代码,我过去的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    • 2012-10-14
    • 1970-01-01
    • 2015-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多