2

他们编写:“支持的平台:可移植类库”。 但后来我写的便携类这段代码中,我有一个错误:在便携库中使用Google API的.NET客户端库

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()”,他doen't便携式图书馆工作。我怎样才能使用库,得到没有GoogleWebAuthorizationBroker的任务。我已经获得自己的访问令牌,我只需要TasksService,也许我可以通过我的访问令牌和其他在构造函数TasksService

References

+0

有什么错误讯息?您是否通过NuGet安装Google API? –

+0

当前上下文中不存在名称'GoogleWebAuthorizationBroker'。 – Denis

+0

等问题。我无法将我的StorageDataStore实现交给GoogleWebAuthorizationBroker。 GoogleWebAuthorizationBroker没有这个参数。 https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth – Denis

回答

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)从谷歌图书馆(Google.Apis.Auth.OAuth2)使用

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

,让您的凭据并遍历它PCL

0

你有两个选择:

1.Declare富为异步方法:

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; 
+0

我忘了添加这个公共async void MyFuncrion()在这里,在我的代码中一切OK。 – Denis

+0

解决方案是什么?我有这个相同的问题。 – Jesse

+0

@Jesse你需要像这样提供你自己的StorageDataStore:https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth 并分离便携式和商店应用程序中的一些代码,我过去的代码 – Denis

相关问题