1

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;
}

これを実行すると、送信されるリクエストは次のとおりです。

http://www.google.com/calendar/feeds/default/private/full?oauth_token=1/kyuMER6kPiG13pOw5M4NoEE7AJdN2fE4jTKSgaMToT8&oauth_consumer_key=419892106817-obg9lu9j9ihpnlh2e2qa01ber4hmq5mh.apps.googleusercontent.com&oauth_nonce=NtfQzRV3&oauth_signature_method=HMAC-SHA1&oauth_signature=XEdf4ipECY2iBfdQfupD1IK1uFw%3D&oauth_version=1.0&oauth_timestamp=1358213228&start -index=1&max-results=5&gsessionid=TV7D3zHTc7T3l41Xqfn-ig

前述のとおり、どちらの場合でも、同じ 401 エラーが発生します。私が間違っていること、またはさらにトラブルシューティングする方法について誰か考えがありますか?

4

1 に答える 1

1

古いバージョンのライブラリを使用していると思います。https://code.google.com/p/google-api-dotnet-client/wiki/Downloadsから最新の安定版リリースをダウンロードしてください 。

また、OAuth2 のドキュメントを読むことも検討する必要があります: https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2

それが役立つことを願っています、Eyal

于 2013-01-23T22:59:40.767 に答える