2

人々に私との約束をスケジュールする機会を与えることを目的とした Web アプリケーションを作成し、ビジネス ロジックを使用して時間が有効であることを検証しました。私はすべての予定を Google カレンダーに記録しています。ということで、Google API Console が作成した Web Application Project に行ってみました。client_id と secret を取得しました。アクセス トークンを正常に取得できます。アクセストークンを自動的に更新するための更新トークンと構築されたロジックもあり、それは正常に機能します。このトークンを使用してカレンダー イベントのリストを取得すると、正常に動作します。次に、トークンを使用して新しいカレンダー イベントを挿入すると、エラーが発生します。最初のコード:

 public CalendarEvent InsertCalendarEvent(CalendarEditArgs args)
 {
 string url = "https://www.googleapis.com/calendar/v3/calendars/" + HttpUtility.UrlEncode(args.CalendarId) + "/events?&key=" + args.APIKey;

 Web.SetUpPost(url, null, true, JsonConvert.SerializeObject(args.Event), null, null, new { Name = "Authorization", Value = "Bearer " + args.AccessToken }, new { Name = "X-JavaScript-User-Agent", Value = "Easy SPS" });
 string response = null;

 try
 {
 response = Web.GetResponse(); 
 return JsonConvert.DeserializeObject<CalendarEvent>(response);
 }
 catch(WebException e)
 {
 response = new StreamReader(e.Response.GetResponseStream()).ReadToEnd();
 throw new Exception(response, e);
 }


 }

 public void SetUpPost(string url, string parameters, bool json, string body, string clientKey, string clientSecret, params object[] data)
 {
 if (body != null && parameters != null)
 throw new InvalidOperationException("You cannot submit a request body and parameters at the same time.");
 request =  WebRequest.Create(url) as HttpWebRequest;


 if (!json)
 {
 request.ContentType = "application/x-www-form-urlencoded";
 }
 else
 {
 request.ContentType = "application/json";
 }
 request.Headers.Add("Accept-Charset", "utf-8");
 request.Method = "POST";
 request.Referer = "https://www.easy-sps.com";
 request.KeepAlive = true;
 byte[] bytes = null;
 if (body != null || parameters != null)
 {
 bytes = System.Text.Encoding.ASCII.GetBytes(body == null ? parameters : body);
 request.ContentLength = bytes.Length;
 System.IO.Stream os = request.GetRequestStream();
 os.Write(bytes, 0, bytes.Length); //Push it out there
 os.Close();
 }
 else
 {
 request.ContentLength = 0;

 }

 if (data != null)
 {
 foreach (var dataObject in data)
 {
 PropertyInfo prop = dataObject.GetType().GetProperties()[0];
 PropertyInfo prop2 = dataObject.GetType().GetProperties()[1];
 request.Headers.Add(prop.GetValue(dataObject, null).ToString(), prop2.GetValue(dataObject, null).ToString());
 }
 }

 }
 }

The error I get:

 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

いくつかの注意事項 clientkey と client secret は実際には使用していません。これらはトークンを取得して使用するために使用されるためです。トークンを取得するには、オフライン アクセスで Google にログインする必要がありました (オンラインでなくてもカレンダーへのアクセスを許可していると Google が示していたため、これが機能したことはわかっています)。私はGoogle Playgroundに行きましたが、そこで動作させることができますが、ここではできません。私が投稿したものと投稿したものの違いを見つけようとしましたが、何も見つかりません。どんな助けでも大歓迎です。1週間以上進歩が見られず、Googleが開発者を直接サポートしていないことがわかっているため、フォーラムを通じてのみ、フォーラムで自分の状況を見つけることができません.

4

2 に答える 2

1

Googleは、カレンダーAPIの.Netラッパーを提供しています。.NETクライアントライブラリ開発者ガイドを参照してください。

于 2012-09-27T21:50:38.207 に答える
0

アクセストークンをURL「access_token =」に入れることで問題を解決しました。URLでは機能するのにヘッダーでは機能しない理由は私にはわかりません。

于 2012-09-28T23:26:55.113 に答える