我一直在修改一段时间,让Google Calendar API与DotNetOpenAuth一起工作。我试着去了解它在几个不同的方式,我一直对以下错误跑起来:401(未经授权)DotNetOpenAuth和谷歌日历错误
The remote server returned an error: (401) Unauthorized.
我已经为.NET谷歌数据API中使用CalendarService试了一下两者,通过调整DotNetOpenAuth示例项目中的Google Contacts示例。
当我尝试使用CalendarService,我建立了我的身份验证服务的配置认证令牌,像这样:
_service = new CalendarService("CalendarSampleApp");
_service.SetAuthenticationToken(accessToken);
当返工的谷歌联系人样,我只是改变了终点,以反映谷歌日历端点,而不是谷歌联系人端点,就像这样:
private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("http://www.google.com/calendar/feeds/default/private/full", HttpDeliveryMethods.GetRequest);
public static XDocument GetCalendarEntries(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/)
{
if (consumer == null)
{
throw new ArgumentNullException("consumer");
}
var extraData = new Dictionary<string, string>() {
{ "start-index", startIndex.ToString(CultureInfo.InvariantCulture) },
{ "max-results", maxResults.ToString(CultureInfo.InvariantCulture) },
};
var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken, extraData);
// Enable gzip compression. Google only compresses the response for recognized user agent headers. - Mike Lim
request.AutomaticDecompression = DecompressionMethods.GZip;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16";
var response = consumer.Channel.WebRequestHandler.GetResponse(request);
string body = response.GetResponseReader().ReadToEnd();
XDocument result = XDocument.Parse(body);
return result;
}
当我运行此,下面是被发送的请求:
正如我所说的,无论在哪种情况下,我都会得到相同的401错误。有没有人有任何想法我做错了什么,或如何进一步排除故障?
我已经下载,并参考了最新版本的图书馆(4.2.1.13026)和我有同样的问题。 – jdmcnair
我误解了上面的答案。我以为你在谈论DotNetOpenAuth库,但你在谈论Google API。我确实是在一个旧版本,我的问题已经与最新版本解决。谢谢! – jdmcnair