1

我正在使用Windows Phone 8應用程序,該應用程序使用Google.Apis nugets。 我在仿真器上調試它時遇到問題(並非我所有的團隊成員都可以訪問設備)。 下面的代碼只是掛indefinetely:Windows Phone - Google API取消[C#]

await Task.Factory.StartNew(() => 
{ 
    try 
    { 
     var result = GoogleWebAuthorizationBroker.AuthorizeAsync(
      new ClientSecrets 
      { 
       ClientId = "<my_client_id>", 
       ClientSecret = "<my_client_secret>" 
      }, 
      new[] {"https://mail.google.com/email"}, 
      "<user_id_to_be_authorized>", 
      token).Result; 
    } 
    catch (Exception ex) 
    { 
     Debug.WriteLine(ex); 
    } 
}); 

如果我改變。結果到.ContinueWith((X)=> {...}),它總是拋出包含在AggregateException一個TaskCanceledException。該代碼在我的Lumia 920上正常工作。有什麼我失蹤?我已經在模擬器中檢查了互聯網連接,瀏覽器正常工作,我也做了一些Google搜索,但沒有結果。

回答

0

試試這個:

private readonly ILogManager _logManager; 
private readonly IStorageService _storageService; 
private UserCredential _credential; 
private Oauth2Service _authService; 
private Userinfoplus _userinfoplus; 

/// <summary> 
/// Initializes a new instance of the <see cref="GoogleService" /> class. 
/// </summary> 
/// <param name="logManager">The log manager.</param> 
/// <param name="storageService">The storage service.</param> 
public GoogleService(ILogManager logManager, IStorageService storageService) 
{ 
    _logManager = logManager; 
    _storageService = storageService; 
} 

/// <summary> 
/// The login async. 
/// </summary> 
/// <returns> 
/// The <see cref="Task"/> object. 
/// </returns> 
public async Task<Session> LoginAsync() 
{ 
Exception exception = null; 
try 
{ 
    // Oauth2Service.Scope.UserinfoEmail 
    _credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets 
    { 
     ClientId = Constants.GoogleClientId, 
     ClientSecret = Constants.GoogleClientSecret 
    }, new[] { Oauth2Service.Scope.UserinfoProfile }, "user", CancellationToken.None); 

    var session = new Session 
    { 
     AccessToken = _credential.Token.AccessToken, 
     Provider = Constants.GoogleProvider, 
     ExpireDate = 
      _credential.Token.ExpiresInSeconds != null 
       ? new DateTime(_credential.Token.ExpiresInSeconds.Value) 
       : DateTime.Now.AddYears(1), 
     Id = string.Empty 
    }; 

    return session; 
} 
catch (TaskCanceledException taskCanceledException) 
{ 
    throw new InvalidOperationException("Login canceled.", taskCanceledException); 
} 
catch (Exception ex) 
{ 
    exception = ex; 
} 
await _logManager.LogAsync(exception); 
return null; 
} 
/// <summary> 
/// Gets the user information. 
/// </summary> 
/// <returns> 
/// The user info. 
/// </returns> 
public async Task<Userinfoplus> GetUserInfo() 
{ 
    _authService = new Oauth2Service(new BaseClientService.Initializer() 
    { 
     HttpClientInitializer = _credential, 
     ApplicationName = AppResources.ApplicationTitle, 
    }); 
    _userinfoplus = await _authService.Userinfo.V2.Me.Get().ExecuteAsync(); 

    return _userinfoplus; 
} 
+0

在原來的職位編輯刪除了,所以我會在這裏解釋: 我已經找到了問題的原因 - 在8.0 WP模擬器我無法得到它的工作,但安裝8.1 WP SDK與新的模擬器解決了這個問題(在8.1模擬器上工作正常) –