无法使用.NET客户端库检索Google Group的设置

时间:2013-07-18 14:17:44

标签: .net google-api-dotnet-client google-groups-settings

我目前正在编写一个系统,负责创建和维护Google网上论坛,使其与我们的内部系统相关联(并与之同步)。

作为其中的一部分,我目前正致力于创建一个组,更改其设置,然后将一些成员分配给该组。

到目前为止,第一部分正常运行,但第二部分 - 使用Google网上论坛设置API - 失败。它似乎总是在期待JSON时接收XML数据。这导致无法反序列化,从而引发异常。

我有客户端库的最新版本(在撰写本文时): Google.Apis.Groupssettings.v1 1.4.0.28227(1.4.0-beta)

这是一些失败的示例代码:

// OAuth2.0/service account stuff here
var initializer = //...;
var settingsService = new GroupssettingsService(initializer);
var settings = settingsService.Groups.Get("samplegroup@example.com").Execute();

一直都很好,直到最后一行失败并出现以下错误:

  • GoogleApiException:发生错误,但无法反序列化错误响应。
  • InnerException:Newtonsoft.Json.JsonReaderException:解析值时遇到意外的字符:<。路径'',第0行,第0位。

使用Fiddler,我发现这是响应:

<?xml version="1.0" encoding="UTF-8"?>
<errors xmlns="http://schemas.google.com/g/2005">
 <error>
  <domain>GData</domain>
  <code>invalid</code>
  <internalReason>A system error has occurred</internalReason>
 </error>
</errors>

我认为错误可能的事实可归结为新创建的群组这一事实,但我也尝试使用旧版本,并获得以下内容:

HTTP/1.1 200 OK
Expires: Thu, 18 Jul 2013 13:00:13 GMT
Date: Thu, 18 Jul 2013 13:00:13 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
ETag: "w9Sr8O0S9lDi5Pcv_43hXQkUtmA/TS0CjusfGhj0vG_aNIJAXkmNM4s"
Content-Type: application/atom+xml; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 1811
Server: GSE

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" xmlns:gd="http://schemas.google.com/g/2005">
 <id>tag:googleapis.com,2010:apps:groupssettings:GROUP:examplegrp@example.com</id>
 <title>Groups Resource Entry</title>
 <content type="text">An example group</content>
 <author>
  <name>Google</name>
 </author>
 <apps:email>examplegrp@example.com</apps:email>
 <apps:name>An example group</apps:name>
 <apps:description/>
 <apps:whoCanJoin>CAN_REQUEST_TO_JOIN</apps:whoCanJoin>
 <apps:whoCanViewMembership>ALL_MANAGERS_CAN_VIEW</apps:whoCanViewMembership>
 <apps:whoCanViewGroup>ALL_MEMBERS_CAN_VIEW</apps:whoCanViewGroup>
 <apps:whoCanInvite>ALL_MANAGERS_CAN_INVITE</apps:whoCanInvite>
 <apps:allowExternalMembers>false</apps:allowExternalMembers>
 <apps:whoCanPostMessage>ANYONE_CAN_POST</apps:whoCanPostMessage>
 <apps:allowWebPosting>true</apps:allowWebPosting>
 <apps:maxMessageBytes>5242880</apps:maxMessageBytes>
 <apps:isArchived>false</apps:isArchived>
 <apps:archiveOnly>false</apps:archiveOnly>
 <apps:messageModerationLevel>MODERATE_NONE</apps:messageModerationLevel>
 <apps:spamModerationLevel>MODERATE</apps:spamModerationLevel>
 <apps:replyTo>REPLY_TO_IGNORE</apps:replyTo>
 <apps:customReplyTo/>
 <apps:sendMessageDenyNotification>false</apps:sendMessageDenyNotification>
 <apps:defaultMessageDenyNotificationText/>
 <apps:showInGroupDirectory>false</apps:showInGroupDirectory>
 <apps:allowGoogleCommunication>false</apps:allowGoogleCommunication>
 <apps:membersCanPostAsTheGroup>false</apps:membersCanPostAsTheGroup>
 <apps:messageDisplayFont>DEFAULT_FONT</apps:messageDisplayFont>
 <apps:includeInGlobalAddressList>true</apps:includeInGlobalAddressList>
</entry>

所以,即便如此,它仍然不能反序列化,因此无效。

我做错了什么,如果有的话?

1 个答案:

答案 0 :(得分:3)

.NET客户端库不支持xml,而Groupssettings API支持atom和json。我的建议是做以下事情:

var getRequest = settingsService.Groups.Get("samplegroup@example.com");
getRequest.Alt = "json";
var settings = getRequest.Execute();