简体   繁体   中英

Windows Phone - Google API gets cancelled [C#]

I'm working on a Windows Phone 8 app, which uses the Google.Apis nugets. I have a problem with debugging it on emulator (not all my team members have access to a device). The following code just hangs indefinetely:

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

And if I change .Result to .ContinueWith((x) => {...}) it always throws a TaskCanceledException contained in AggregateException. The code works fine on my Lumia 920. Is there something I am missing? I've checked the internet connection in emulator and the browser works, I've also done some googling, but with no results.

Try this :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM