Google Calendar API を DotNetOpenAuth で動作させることについて、しばらくいじくり回してきました。私はいくつかの異なる方法でそれを試してみましたが、次のエラーに遭遇し続けています:
The remote server returned an error: (401) Unauthorized.
Google Data API for .NET で CalendarService を使用する方法と、DotNetOpenAuth サンプル プロジェクトの Google Contacts サンプルを適合させる方法の両方を試しました。
CalendarService を使用しようとしたときに、次のように認証トークンを設定して、認証用のサービスをセットアップしました。
_service = new CalendarService("CalendarSampleApp");
_service.SetAuthenticationToken(accessToken);
Google コンタクトのサンプルを作り直すとき、次のように、Google コンタクト エンドポイントではなく、Google カレンダー エンドポイントを反映するようにエンドポイントを変更しました。
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 エラーが発生します。私が間違っていること、またはさらにトラブルシューティングする方法について誰か考えがありますか?