2

They write:"Supported Platforms: Portable Class Libraries". But then I write this code in portable class, I have a error:

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.
            }
}

Error here: "UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync()", He doen't work in portable library. How can I use library, get tasks without GoogleWebAuthorizationBroker. I already get access token myself and I need only TasksService, maybe I can past my access token and other in the constructor TasksService?

References

Denis
  • 2,622
  • 3
  • 22
  • 24
  • What's the error message? Did you install the Google API via NuGet? – Stephen Cleary Apr 08 '14 at 18:32
  • The name 'GoogleWebAuthorizationBroker' does not exist in the current context. – Denis Apr 08 '14 at 18:41
  • And other question. I can't give my implement of StorageDataStore to the GoogleWebAuthorizationBroker. GoogleWebAuthorizationBroker doen't have this parametr. https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth – Denis Apr 08 '14 at 18:44
  • Did you install it via NuGet? Have you referenced the namespace `Google.Apis.Auth.OAuth2`? – Stephen Cleary Apr 08 '14 at 18:46

2 Answers2

1

I use this article to breake this wall google .net library

Here i past some my code from PCL and windows8.

PCL: You need provide 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;
}

Then

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) In Windows store you need provide StorageDataStore and traverse it to pcl. 2) Use

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

from google library (Google.Apis.Auth.OAuth2) to get your credential and traverse it to pcl

Denis
  • 2,622
  • 3
  • 22
  • 24
0

You have two options:

1.Declare foo as async method:

 async void Foo()

2.Remove await, and get the result of your task, so your code should look something like:

 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;
peleyal
  • 3,472
  • 1
  • 14
  • 25
  • I forget add this public async void MyFuncrion() here, in my code all OK. – Denis Apr 08 '14 at 19:43
  • What was the solution? I am having this same problem. – Jesse Jul 30 '14 at 13:46
  • @Jesse You need provide you own StorageDataStore like here: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth And separate some code in portable and store app, i past code – Denis Sep 11 '14 at 22:01