2

我在使用 .Net 的 Google api 时遇到了一些问题。当调用服务并且由于某种原因调用失败时,会引发 GoogleApiException。HttpStatusCode 在异常中始终为 0。我可以从 Message 中解析状态代码,但这当然不是一个好的解决方案。

我正在使用来自 NuGet (1.5.0.28972) 的最新二进制文件

我在让 ExponentialBackoff 算法工作时也遇到了问题,我认为这与这个问题有关。

下面是复制行为的 som 代码。

    const string ServiceAccount = "000000000000@developer.gserviceaccount.com";
    const string KeyFile = @"C:\privatekey.p12";
    const string KeyPass = "notasecret";

    const string ApplicationName = "Test";
    const string User = "admin@some-domain.com";  // this is the user you wish to act for


static void Main(string[] args)
{
    var certificate = new X509Certificate2(KeyFile, KeyPass,
                                           X509KeyStorageFlags.Exportable);

    var provider = new AssertionFlowClient(GoogleAuthenticationServer.Description, certificate)
    {
        ServiceAccountId = ServiceAccount,
        Scope = DirectoryService.Scopes.AdminDirectoryUser.GetStringValue(),
        ServiceAccountUser = User,
    };


    var authenticator = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);

    var initializer = new BaseClientService.Initializer
    {
        Authenticator = authenticator,
        DefaultExponentialBackOffPolicy = BaseClientService.ExponentialBackOffPolicy.UnsuccessfulResponse503
    };

    var service = new DirectoryService(initializer);

    // This will throw a GoogleApiException the the HttpStatusCode is 0 (Should be 409)
    var user = service.Users.Get("none-existing-user@some-domain.com").Execute();
}

问候/托

4

0 回答 0