我正在尝试按照文档通过 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 有效且未过期,应用程序密钥正确。
我做错了什么或错过了什么?
提前致谢,