2009年11月12日 星期四

使用 Google protrol to get the google api in C# with Google Site data api

在這個範例 中 使用 Google protocol 去取得 google site data 的資訊

所使用的語言是 C#, java的版本之前在其他文章有寫過


Http Post requset with java:
使用 java http post 取得 google account (AuthSub)

Http Get request with java:
使用 java http Get( by AuthSub) 取得 google document list





主要分成3個部份

1. SetProxyServer (有經過proxy 才要設 )
code 如下

/**
* @name SetProxyServer
*
* Set the Proxy Server if have Proxy Server
*
* @param req [in] - the http web request
*/
public void SetProxyServer(HttpWebRequest req)
{

IWebProxy iProxy = WebRequest.DefaultWebProxy;

/// potentially, setup credentials on the proxy here
iProxy.Credentials = CredentialCache.DefaultCredentials;
req.Proxy = iProxy;
}






2.
取得 google api 的要驗證碼( 使用 http post), 這裡我是使用client login的方法

輸入以下三種資訊
Account: google 帳號
Password: google 帳號的密碼
Service Type: google api 的 類型 這裡使用 google site 類型為 "jotspot" (其他類型 )

code 如下



/**
* @name GetGoogleLoginAuth
*
* Get the google client login auth token
*
* @param Account [in] - the google account
* @param Password [in] - the google account password
* @param ServiceType [in] - the google api type
*
* @return the google client login auth
*/
public string GetGoogleLoginAuth(string Account, string Password, string ServiceType)
{
/// Declare the return valure
string GoogleLoginAuth="";

/// Create the request obj
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin");

/// Set proxy
this.SetProxyServer(req);

/// Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
string strNewValue = "accountType=HOSTED_OR_GOOGLE"+
"&Email=" + Account +
"&Passwd=" + Password +
"&service=" + ServiceType +
"&source=Gulp-CalGulp-1.05";

/// Set the request value
req.ContentLength = strNewValue.Length;


/// Write the request
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
stOut.Write(strNewValue);
stOut.Close();


/// Do the request to get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = stIn.ReadToEnd();


/// Split string and set token
/// there are tree type in the return value
/// SID, LSID and Auth
string[] parts = strResponse.Split('=');

/// Save the auth token, auth token is in last part of string parts[]
GoogleLoginAuth = parts[parts.Length - 1];

/// Close the stream reader
stIn.Close();


return GoogleLoginAuth;
}




3. RetrievalContentFeed , 利用 http get 取得google site 的資訊
主要實做 google guide 的範例

輸入兩個 資訊
a. url: http(s)://sites.google.com/feeds/content/site/siteName

site 網域名稱 如 example.com
siteName 網頁名稱 如 myCoolSite

b. token 剛取得的 auth token


附註:
要 傳送出以下的訊息
GET /feeds/content/site/siteName HTTP/1.1
Host: sites.google.com
GData-Version: 1.0
Authorization: GoogleLogin auth= yourAuthToken

在c # 這裡是用 HttpWebRequest.Headers.Add 來附加訊息
此外 Host 不需在設定, 因為 會從 url 取得
所以要附加的資訊就只有
GData-Version 和 Authorization

看下面的code 會更瞭解, code 如下


/**
* @name RetrievalContentFeed
*
* Get the google client login auth token
*
* @param URL [in] - the google api Url
* @param Token [in] - the google client login token
*
*
* @return Content Feed
*/
public string RetrievalContentFeed(string URL, string Token)
{
/// Declare the return valure
string Feed = "";

/// Create the request obj
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://sites.google.com/feeds/content/hh-mud.com/touch-teach");

/// Set proxy
this.SetProxyServer(req);

/// Set values for the request back
req.Method = "GET";

/// Set the Head value
req.Headers.Add("GData-Version:1.0");
req.Headers.Add("Authorization: GoogleLogin auth=" + Token);


/// Do the request to get the response
StreamReader stIn = new StreamReader(req.GetResponse().GetResponseStream());
Feed = stIn.ReadToEnd();

stIn.Close();

return Feed;
}


沒有留言: