使用Google Admin SDK的Google用户管理,vb.net和服务帐户正在开发中,但无法在生产环境中使用

问题描述

我需要使用Google Admin SDK目录服务将用户添加到我们的gsuite域中,我正在使用服务帐户,语言是vb.net。我的应用程序在Visual Studio中运行良好,但是一旦将其发布到IIS服务器上,我就会get。 我不知所措,并且一直在挣扎着挣扎数月之久。我联系了google cloud支持,他们认为我的代码对他们来说还不错,但无法解决特定的编码问题。他们在这里介绍我。
这是我的代码

Try
    ' service account email address
    Const serviceAccount As String = "My service account"

    ' import service account key p12 certificate.
      
    Dim certificate = New X509Certificate2("c:\googletest\accountmgmt-************.p12","notasecret",X509KeyStorageFlags.Exportable)
                
    ' G Suite user email address
    Dim gsuiteUser = "My Superadmin Account"

    Dim serviceAccountCredentialInitializer = New ServiceAccountCredential.Initializer(serviceAccount) With {
            .User = gsuiteUser,.Scopes = {"https://www.googleapis.com/auth/admin.directory.user"}
        }.FromCertificate(certificate)

   
    ' request access token
    Dim credential = New ServiceAccountCredential(serviceAccountCredentialInitializer)
    If Not credential.RequestAccesstokenAsync(CancellationToken.None).Result Then 
        Throw New InvalidOperationException("Access token Failed.")
    End If
    Dim service = New DirectoryService(New BaseClientService.Initializer() With {
                .HttpClientinitializer = credential,.ApplicationName = "AccountMgmt"
            })
    
    Dim user As User = New User()
    Dim Name As UserName = New UserName
    Name.FamilyName = "Testy"
    Name.Givenname = "Test1"
    user.Name = Name
    user.Password = "Password123!"
    user.PrimaryEmail = "[email protected]"
    Dim results As User = service.Users.Insert(user).Execute()
       

Catch ep As Exception
    Console.WriteLine(ep.ToString())
End Try

正如我说的那样,它在Visual Studio的开发箱中可以正常工作,但是当我将其发布到IIS服务器并执行时,我没有迹象表明它会执行任何操作,也不会创建帐户。 vb.net或c#.net中的任何建议将不胜感激

解决方法

我可以看到您正在尝试使用系统路径“ c:...”加载证书,这很可能会返回IO错误。尝试将路径更改为“ Server.MapPath()”以加载证书。