在便携式库中使用Google API Client Library for .NET

时间:2014-04-08 17:04:28

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

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

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中的其他令牌?

References

2 个答案:

答案 0 :(得分:1)

我使用这篇文章来破坏这面墙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商店中,您需要提供StorageDataStore并将其遍历到pcl。 2)使用

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

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

答案 1 :(得分:0)

您有两种选择:

1.Declare foo as async method:

 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;