2011-12-16 10 views
1

私は自分のGoogleカレンダーをv3 API(http://code.google.com/p/google-api-dotnet-client/)で読もうとしています。私が最終的に望むのは、自分のサイト上のカスタムコントロールを使って自分のカレンダーをGoogleカレンダーから表示することです(その後、カスタム予定を作ることができます)。私が読んでいるすべてのサンプルは、手動でデータにアクセスする必要があるという前提に基づいています。 Google APIコンソールでは、APIキーを使用してアプリケーションを作成しました。私は自分のコードのために有効なIAuthenticationを作成しようとしています。決して私が使用しなければならない認証は得られません。以下は悲惨な試行です、誰がこれを手伝ってくれるのですか?私このためGoogle API v3 for dotnet;

public void TestMethod1() 
    { 
     // http://code.google.com/p/google-api-dotnet-client/wiki/OAuth2 
     NativeApplicationClient provider = NativeApplicationClient(GoogleAuthenticationServer.Description); 
     NativeApplicationClient(GoogleAuthenticationServer.Description); 

     provider.ClientIdentifier = "x"; 
     provider.ClientSecret = "x"; 

     var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); 


     Google.Apis.Calendar.v3.CalendarService service = new Google.Apis.Calendar.v3.CalendarService(auth); 
     var result = service.Events.List("primary").Fetch(); 

     Assert.IsTrue(result.Items.Count > 0); 
    } 

    private static IAuthorizationState GetAuthorization(NativeApplicationClient arg) 
    { 
     IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() }); 

     return arg.ProcessUserAuthorization("", state); 
    } 

答えて

3

こんにちは作品:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Google.Apis.Calendar; 
using Google.Apis.Calendar.v3; 
using Google.Apis.Authentication; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using DotNetOpenAuth.OAuth2; 
using System.Diagnostics; 
using Google.Apis.Calendar.v3.Data; 

namespace consoleGoogleResearch 
{ 
    class Program 
    { 
     public static void Main(string[] args) 
     {    
      // Register the authenticator. The Client ID and secret have to be copied from the API Access 
      // tab on the Google APIs Console. 
      var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description); 
      provider.ClientIdentifier = "fill_in_yours"; 
      provider.ClientSecret = "fill_in_yours";   
      // Create the service. This will automatically call the authenticator. 
      var service = new CalendarService(new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication)); 

      Google.Apis.Calendar.v3.CalendarListResource.ListRequest clrq = service.CalendarList.List(); 
      var result = clrq.Fetch(); 

      if (result.Error != null) 
      { 
       Console.WriteLine(result.Error.Message); 
       Console.ReadKey(); 
       return; 
      } 

      Console.WriteLine("Calendars: ");   
      foreach (CalendarListEntry calendar in result.Items) 
      { 
       Console.WriteLine("{0}", calendar.Id); 
       Console.WriteLine("\tAppointments:"); 
       Google.Apis.Calendar.v3.EventsResource.ListRequest elr = service.Events.List(calendar.Id); 
       var events = elr.Fetch(); 
       foreach (Event e in events.Items) 
       { 
        Console.WriteLine("\t From: {0} To: {1} Description: {2}, Location: {3}", e.Start, e.End, e.Description, e.Location); 
       } 
      }    
      Console.ReadKey(); 
     }   

     private static IAuthorizationState GetAuthentication(NativeApplicationClient arg) 
     {    
      // Get the auth URL: 
      //IAuthorizationState state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.ToString() }); 
      IAuthorizationState state = new AuthorizationState(new[] { "https://www.google.com/calendar/feeds" }); 
      state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl); 
      Uri authUri = arg.RequestUserAuthorization(state); 

      // Request authorization from the user (by opening a browser window): 
      Process.Start(authUri.ToString()); 
      Console.Write(" Authorization Code: "); 
      string authCode = Console.ReadLine(); 

      // Retrieve the access token by using the authorization code: 
      return arg.ProcessUserAuthorization(authCode, state); 
     } 
    } 
}