我正在尝试按照documentation通过Google API创建日历。我试图避免使用客户端库并通过自定义webrequests与API进行所有通信,到目前为止一直运行良好,但在这个特定的一个我正在努力解决“解析错误”。
请不要参考使用客户端库的解决方案( service.calendars()。insert(...))。
这是我的代码的一个愚蠢版本(仍然无效):
var url = string.Format
(
"https://www.googleapis.com/calendar/v3/calendars?key={0}",
application.Key
);
var httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Headers["Authorization"] =
string.Format("Bearer {0}", user.AccessToken.Token);
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.CookieContainer = new CookieContainer();
// Obviously the real code will serialize an object in our system.
// I'm using a dummy request for now,
// just to make sure that the problem is not the serialization.
var requestText =
"{" + Environment.NewLine
+ "\"summary\": \"test123\"" + Environment.NewLine
+ "}" + Environment.NewLine
;
using (var stream = httpWebRequest.GetRequestStream())
using (var streamWriter = new System.IO.StreamWriter(stream))
{
streamWriter.Write(System.Text.Encoding.UTF8.GetBytes(requestText));
}
// GetSafeResponse() is just an extension that catches the WebException (if any)
// and returns the WebException.Response instead of crashing the program.
var httpWebResponse = httpWebRequest.GetSafeResponse();
正如你所看到的,我已经放弃了现在发送序列化对象的问题,而我只是试图让它处理一个非常简单的虚拟请求:
{
"summary": "test123"
}
然而,答案仍然只是:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "parseError",
"message": "Parse Error"
}
],
"code": 400,
"message": "Parse Error"
}
}
accessToken有效且未过期,应用程序密钥正确。
我做错了什么或错过了什么?
提前致谢,
答案 0 :(得分:2)
我不确定这会解决您的问题,但需要注意几点 在这种情况下,请不要使用Environment.NewLine,如果您的代码在Windows,Mac或Linux上运行,则不应更改网络流量。 Http 1.1需要CRLF
您正在将帖子正文编码为UTF-8是的,您没有告诉服务器您正在使用哪种编码。你的所有字符都是低位ASCII,所以这不重要,但为了完整,你的内容类型应该是
httpWebRequest.ContentType = "application/json ; charset=UTF-8";
除此之外我无法看到您的代码存在问题,最好是附上一个透明的回显代理(Charles或fiddler),以便您可以通过网络查看您的请求是什么样的。来自日历examples,他们正在发送
POST https://www.googleapis.com/calendar/v3/calendars?key={YOUR_API_KEY}
Content-Type: application/json
Authorization: Bearer ya29.AHES6ZR3F6ByTg1eKVkjegKyWIukodK8KGSzY-ea1miGKpc
X-JavaScript-User-Agent: Google APIs Explorer
{
"summary": "Test Calendar"
}
200 OK
- Show headers -
{
"kind": "calendar#calendar",
"etag": "\"NybCyMgjkLQM6Il-p8A5652MtaE/ldoGyKD2MdBs__AsDbQ2rHLfMpk\"",
"id": "google.com_gqua79l34qk8v30bot94celnq8@group.calendar.google.com",
"summary": "Test Calendar"
}
希望有所帮助,意识到它可能没有。
答案 1 :(得分:1)
我想出来并让它发挥作用!
虽然大卫的建议并不是他们自己的解决方案,但是他告诉我使用数据包嗅探器(我最终使用的是Wireshark,但这不是真正的重点),这让我走上正轨。
事实证明,在我愚蠢的代码中有两个错误。一个如此明显的明显,它让我脸红,一个稍微狡猾。
首先,
using (var streamWriter = new StreamWriter(stream))
{
streamWriter.Write(Encoding.UTF8.GetBytes(requestText));
}
当然应该是
using (var streamWriter = new StreamWriter(stream, Encoding.UTF8))
{
streamWriter.Write(requestText);
}
作为streamWriter.Write对参数和Byte []执行ToString()。ToString()只返回“System.Byte []”。尴尬!
其次,默认的UTF8编码添加字节顺序标记\ 357 \ 273 \ 277,就谷歌而言,这也会使内容无效。我找到了如何在stackoverflow上修复此问题here。
因此,对于任何挣扎于此的人来说,这是最终的解决方案。
var url = string.Format
(
"https://www.googleapis.com/calendar/v3/calendars?key={0}",
application.Key
);
var httpWebRequest = HttpWebRequest.Create(url) as HttpWebRequest;
httpWebRequest.Headers["Authorization"] =
string.Format("Bearer {0}", user.AccessToken.Token);
httpWebRequest.Method = "POST";
// added the character set to the content-type as per David's suggestion
httpWebRequest.ContentType = "application/json; charset=UTF-8";
httpWebRequest.CookieContainer = new CookieContainer();
// replaced Environment.Newline by CRLF as per David's suggestion
var requestText = string.Join
(
"\r\n",
"{",
" \"summary\": \"Test Calendar 123\"",
"}"
);
using (var stream = httpWebRequest.GetRequestStream())
// replaced Encoding.UTF8 by new UTF8Encoding(false) to avoid the byte order mark
using (var streamWriter = new StreamWriter(stream, new UTF8Encoding(false)))
{
streamWriter.Write(requestText);
}
希望这有助于某人!