Search code examples
c#windows-phone-8portable-class-librarygoogle-api-dotnet-clientgoogle-tasks-api

Using Google APIs Client Library for .NET in portable library


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


Solution

  • 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