Windows Phone - Google API gets cancelled [C#]

本文关键字:cancelled gets API Phone Google Windows | 更新日期: 2023-09-27 18:08:27

我正在开发一个Windows Phone 8应用程序,它使用的是Google。nuget api。我在模拟器上调试它时遇到了问题(不是所有的团队成员都可以访问设备)。下面的代码只是无限期挂起:

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);
    }
});

如果我改变。result到。continuewith ((x) =>{…})它总是抛出一个TaskCanceledException包含在AggregateException。代码在我的Lumia 920上运行良好。我是不是漏掉了什么?我已经检查了模拟器中的互联网连接和浏览器的工作,我也做了一些谷歌搜索,但没有结果。

Windows Phone - Google API gets cancelled [C#]

试试这个:

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