2009年7月28日 星期二

Google Api in .Net 實做

以下我是用 VS2008 和 win XP 為環境做的簡單程式


基本上照這裡的步驟就OK 了, 教學網址


流程
1. 下載 Google Data API Setup.msi .NET Client Library 現在是(1.4.0.2 )
2. 開啟新專案, 這裡我是用 WPF 的專案, 不過沒有差別,基本上都是用C#寫(.CS檔)
3. Add Reference 如圖

4. 選擇Browse tag , 尋找Google api SDK 的路徑 , 預設都會將相關的DLL在 C:\Program Files\Google\Google Data API SDK\Redist


5. 選擇要引用的DLL, 這裡要用的有 Google.GData.Client.dll, Google.GData.Extensions.dll 這兩個DLL , 此外, 因為是以 Calendar 為範例 所以也要引用 Google.GData.Calendar.dll

6. 引用完這些DLL , 但還會無法執行,因為驗證沒過,所以 必須還要在引用 Google.GData.AccessControl.dll


在這個簡單例子我是用這個 UI , 按一下 click me 就會顯示google Calendar 的日曆清單


code 如下

//-------------------------------------------------------
// Window1.xaml.cs
//-------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace WPFGoogle
{

/// Interaction logic for Window1.xaml

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}


void button_Click(object sender, RoutedEventArgs e)
{

CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("Your@gmail.com", "YourPassword");

CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/allcalendars/full");

String Temp;

CalendarFeed resultFeed = myService.Query(query);

/// Get calnedars List
Temp = "Your calendars:\n";
foreach (CalendarEntry entry in resultFeed.Entries)
{
Temp = Temp + entry.Title.Text + "\n";

}

/// Show the Message
MessageBox.Show(Temp);


}

}
}


萬一需要設定 proxy 則 必須 修改 code 如下

//-------------------------------------------------------
// Window1.xaml.cs
//-------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

using System.Net;

namespace WPFGoogle
{

/// Interaction logic for Window1.xaml

public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}


void button_Click(object sender, RoutedEventArgs e)
{

CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
myService.setUserCredentials("Your@gmail.com", "YourPassword");

CalendarQuery query = new CalendarQuery();
query.Uri = new Uri("http://www.google.com/calendar/feeds/default/allcalendars/full");


/// Set proxy

GDataRequestFactory requestFactory = (GDataRequestFactory)myService.RequestFactory;

IWebProxy iProxy = WebRequest.DefaultWebProxy;

WebProxy myProxy = new WebProxy(iProxy.GetProxy(query.Uri));

// /potentially, setup credentials on the proxy here

myProxy.Credentials = CredentialCache.DefaultCredentials;

myProxy.UseDefaultCredentials = true; requestFactory.Proxy = myProxy;





String Temp;

CalendarFeed resultFeed = myService.Query(query);

Temp = "Your calendars:\n";
/// Get calnedars List
foreach (CalendarEntry entry in resultFeed.Entries)
{
Temp = Temp + entry.Title.Text + "\n";

}

/// Show the Message
MessageBox.Show(Temp);


}

}
}

沒有留言: