簡體   English   中英

Windows Phone-Google API被取消[C#]

[英]Windows Phone - Google API gets cancelled [C#]

我正在使用Windows Phone 8應用程序,該應用程序使用Google.Apis nuget。 我在模擬器上調試它時遇到了問題(並非我的所有團隊成員都可以訪問設備)。 以下代碼無限期地掛起:

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)=> {...}),它將始終引發AggregateException中包含的TaskCanceledException。 該代碼可以在我的Lumia 920上正常工作。我缺少什么嗎? 我檢查了模擬器中的互聯網連接,並且瀏覽器正常工作,我也進行了一些谷歌搜索,但沒有結果。

嘗試這個 :

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM