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

相關問題