我试图访问谷歌API V3 Calendarlist但我不断收到四百零四分之四百零一反应,即使我有一个有效的accessToken的用户。
(我完成了对Web服务器应用程序的OAuth2协议 ,并获得https://www.googleapis.com/auth/calendar许可)
它已经很难找到良好的文档对于这一点,谷歌的网站本身是不是非常有帮助,并搜索都充满了旧v2的信息,或使用的SDK。 通常我可以做翻译蟒蛇答复C#,但在这种情况下,即使没有帮助我。
所以我拉我的头发,但我可能忘了什么东西很简单这里就像一个缺少参数或诸如此类,所以它如果有人将采取快速浏览一下这些代码,并告诉我,我做错了什么不胜感激。
var applicationKey = moduleModel.Application.Key;
var userID = moduleModel.User.UserID;
var accessToken = moduleModel.User.AccessToken.Token;
// It doesn't seem to make any difference whether I use the querystring
// parameter or the request header to specify the access_token
var sendAccessTokenAsHeader =
!(Request.QueryString["SendAccessTokenAsHeader"] == "0");
// I tried navigating to a specific user but it results in a 404 Not Found
var requestSpecificUserID =
Request.QueryString["RequestSpecificUserID"] == "1";
// Including or omitting my API_KEY doesn't seem to make any difference
var sendApplicationKey =
!(Request.QueryString["SendApplicationKey"] == "0");
var urlBuilder = new System.Text.StringBuilder();
urlBuilder.Append("https://");
urlBuilder.Append("www.googleapis.com");
if (requestSpecificUserID)
{
urlBuilder.Append
(string.Format("/calendar/v3/users/{0}/calendarList", userID));
}
else
{
urlBuilder.Append
("/calendar/v3/users/me/calendarList");
}
var parameterJoiner = "?";
if (sendApplicationKey)
{
urlBuilder.Append
(
string.Format
(
"{0}{1}={2}",
parameterJoiner,
"key",
HttpUtility.UrlEncode(applicationKey)
)
);
parameterJoiner = "&";
}
if (!sendAccessTokenAsHeader)
{
urlBuilder.Append
(
string.Format
(
"{0}{1}={2}",
parameterJoiner,
"access_token",
HttpUtility.UrlEncode(accessToken)
)
);
parameterJoiner = "&";
}
var httpWebRequest = HttpWebRequest.Create(urlBuilder.ToString())
as HttpWebRequest;
httpWebRequest.CookieContainer = new CookieContainer();
if (sendAccessTokenAsHeader)
{
httpWebRequest.Headers["Authorization"] = string.Format
(
"Bearer {0}",
accessToken
);
}
// GetSafeResponse is just an extension method to catch the WebException
// when the HttpStatusCode != OK
var response = httpWebRequest.GetSafeResponse();
// returns 401 (requestSpecificUserID = false)
// or 404 (requestSpecificUserID = true)
var responseText = response.GetResponseText();
return responseText;
先感谢您!