【问题标题】:Google directory api org unit error 404Google 目录 api 组织单位错误 404
【发布时间】:2013-09-28 05:24:53
【问题描述】:

我正在通过 Google .net 客户端库为组织单位调用获取、更新和删除 API,但得到 404。我使用了 Fiddler,发现请求 URL 格式不正确。我在 URL 中看到 {/orgUnitPath*} 字符串而不是我的组织单位路径,并且客户 ID 被替换为实际的客户 ID,我在 Fiddler 中使用实际的组织单位路径提出了该请求,它工作正常。

我的组织单位路径是 ABC/IT,我认为初始化可重复字符串存在一些问题,因为我的插入和列表 API 工作正常:

Repeatable<string> rep = new Repeatable<string>(new List<string>{orgUnitPath});

OrgunitsResource.GetRequest gr = service.Orgunits.Get(GetGoogleUser(ConfigManager.AdminIdentity, accessToken).CustomerId, rep);

OrgUnit orgUnit = gr.Fetch();

我在这里做错了吗?

我更新客户端库后的新代码是:

public OrgUnit GetGoogleOrganizationUnit(string orgUnitPath, string accessToken)
        {
            AccessToken = accessToken;
            var service = new DirectoryService(GetGoogleServiceClient());

            Repeatable<string> rep = new Repeatable<string>(new List<string> { orgUnitPath });

            OrgunitsResource.GetRequest gr = service.Orgunits.Get(GetGoogleUser(ConfigManager.AdminIdentity, accessToken).CustomerId, rep);

            OrgUnit orgUnit = gr.Execute();

            return orgUnit;
        }

以下是堆栈跟踪:

[JsonReaderException: Error parsing NaN value. Path '', line 0, position 0.]
   Newtonsoft.Json.JsonTextReader.ParseNumberNaN() +97
   Newtonsoft.Json.JsonTextReader.ParseValue() +400
   Newtonsoft.Json.JsonTextReader.ReadInternal() +35
   Newtonsoft.Json.JsonTextReader.Read() +20
   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter) +74
   Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) +442
   Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) +687
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +111
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +66
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value) +42
   Google.Apis.Json.NewtonsoftJsonSerializer.Deserialize(String input) in c:\code.google.com\google-api-dotnet-client\default_3\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis\Json\NewtonsoftJsonSerializer.cs:72
   Google.Apis.Services.<DeserializeError>d__9.MoveNext() in c:\code.google.com\google-api-dotnet-client\default_3\Tools\Google.Apis.Release\bin\Debug\output\default\Src\GoogleApis\Apis\Services\BaseClientService.cs:357

[GoogleApiException: An Error occurred, but the error response could not be deserialized]
   BLL.GoogleAPIManagerBLL.GetGoogleOrganizationUnit(String orgUnitPath, String accessToken) in c:\Projects\FGPortal\BLL\GoogleAPIManagerBLL.cs:504
   Application.ManageOrgUnits.gvorgunits_RowCommand(Object sender, GridViewCommandEventArgs e) in c:\Projects\FGPortal\Application\ManageOrgUnits.aspx.cs:29
   System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e) +111
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +73
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e) +121
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +156
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +9642898
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

【问题讨论】:

    标签: asp.net google-admin-sdk google-api-dotnet-client


    【解决方案1】:

    首先,您使用的是旧版本的库。请使用 NuGet 执行以下操作:

    然后尝试执行以下操作:

    OrgUnit orgUnit = service.Orgunits.Get(CustomerId).Execute();

    【讨论】:

    • 我在尝试安装包时出现错误:您正在尝试将此包安装到以“.NETFramework,Version=v4.0”为目标的项目中,但该包不包含任何程序集与该框架兼容的引用
    • 当我安装软件包时,它开始给我 .net 框架 4.0 的冲突,即使在 web.config 中使用程序集绑定重定向。
    • 最后我通过删除重定向和引用并再次安装包来让它工作。获取 orgunit 的一个参数没有过载,所以我尝试了两个并得到一个错误“JsonReaderException:解析 NaN 值时出错。路径'',第 0 行,位置 0。”
    • 你能附上你的堆栈跟踪吗?确切的错误是什么?更新问题并包含您拥有的新代码。也许启动一个以 .NET 4.0(或 4.5)为目标的新项目并安装 PRERELEASE NuGet 包实际上是一个好建议。
    • 我已经用新代码和堆栈跟踪更新了这个问题。我的项目已经针对 .Net 4.0
    【解决方案2】:

    最后我使用REST操作来实现它,如果有人遇到解决方案,请告诉我。以下是代码:

    public OrgUnit GetGoogleOrganizationUnit(string orgUnitPath, string accessToken)
            {
                AccessToken = accessToken;
                var service = new DirectoryService(GetGoogleServiceClient());
    
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(String.Format("https://www.googleapis.com/admin/directory/v1/customer/{0}/orgunits/{1}", GetGoogleUser(ConfigManager.AdminIdentity, accessToken).CustomerId, HttpUtility.UrlEncode(orgUnitPath)));
            request.Method = "GET";
            request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + accessToken);
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
    
            var jLinq = JObject.Parse(sr.ReadToEnd());
    
            OrgUnit orgUnit = new OrgUnit();
    
            orgUnit.Kind = jLinq["kind"].ToString();
            orgUnit.ETag = jLinq["etag"].ToString();
            orgUnit.Name = jLinq["name"].ToString();
            orgUnit.Description = jLinq["description"].ToString();
            orgUnit.OrgUnitPath = jLinq["orgUnitPath"].ToString();
            orgUnit.ParentOrgUnitPath = jLinq["parentOrgUnitPath"].ToString();
            orgUnit.BlockInheritance = Convert.ToBoolean(jLinq["blockInheritance"]);
    
            return orgUnit;
        }
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题。但问题出在 orgunitpath 和我的可重复参数上。 orgunitpath 的格式为“/ABC/IT”。一旦我将其更改为“ABC/IT”,它对我来说效果很好。我初始化可重复对象的方式也有一点不同。

      我直接用一个字符串列表代替了可重复参数。

      List<string> list = new List<string>();
      
      list.Add("ABC/IT");
      
      OrgunitsResource.GetRequest orgUnitRequest = googleAppsOAuthService.Orgunits.Get(superAdminImmutableId, list);
      
      Google.Apis.Admin.Directory.directory_v1.Data.OrgUnit orgUnit = orgUnitRequest.Execute();"
      

      【讨论】:

      • 仅供参考,您也可以传递一个字符串,您不必创建一个列表并添加 1 个项目。
      猜你喜欢
      • 2015-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 2016-12-17
      • 1970-01-01
      相关资源
      最近更新 更多