Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I had searched lot of things and site about google fusion table api. Google provide "https://developers.google.com/fusiontables/docs/developers_guide#CreatingTable" this link but i am unable to understand this coding and its process.

Kindly give me a clear sample code about update google fusion tables automatically using web link through C#.net code.
Posted
Updated 22-May-12 23:59pm
v2
Comments
Ashish198 1-Oct-12 3:39am    
any one can please tell me how to fetch data from fusion table through API & how to insert. Answer ASAp

1 solution

Hi,

it's nearly useless to give you sample code if you don't understand what technology is used by Google.
As mentioned by google, the fusion api is build on http and json,so you should be fimilar with GET and POST Methods.

For a starting point, to use this API you have to work with HttpWebResponse and HttpWebRequest Methods in C#.

Example code POST:

C#
private String executeUpdate(String query, String token) {
    string sdata = "sql=" + HttpUtility.UrlEncode(query);
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] data = encoding.GetBytes(sdata);

    // Create the request, encode the query in the body of the post
    HttpWebRequest req = (HttpWebRequest)
      WebRequest.Create("http://tables.googlelabs.com/api/query");
    req.Method = "POST";
    req.ContentType="application/x-www-form-urlencoded";
    req.ContentLength = data.Length;

    // Add the authentication header
    req.Headers.Add("Authorization: GoogleLogin auth=" + token);

    Stream sout = req.GetRequestStream();
    sout.Write(data, 0, data.Length);
    sout.Close();

    try {
      HttpWebResponse res = (HttpWebResponse) req.GetResponse();
      StreamReader stIn = new  StreamReader(res.GetResponseStream());
      return stIn.ReadToEnd();
    }  catch (WebException e) {
      StreamReader stIn = new  StreamReader(e.Response.GetResponseStream());
      return stIn.ReadToEnd();
    }
  }


See googles fusion libs here.

Cheers
 
Share this answer
 
v3
Comments
rameshbabu.S 23-May-12 6:57am    
Dear Björn Ranft,

Please say whats the wrong in my code?
I know that points but my code says "Timeout".
See my code,
string UserName = Convert.ToString(ConfigurationManager.AppSettings["proxyUserName"]);
string Password = Convert.ToString(ConfigurationManager.AppSettings["proxyPassword"]);

protected void Page_Load(object sender, EventArgs e)
{
//val = "https://www.google.com/fusiontables/api/query/?sql=SELECT%20*%20FROM%203602788";
//reqIcon = WebRequest.Create(val);
//if (UserName != string.Empty && Password != string.Empty)
// reqIcon.Proxy.Credentials = new System.Net.NetworkCredential(UserName, Password);
//resIcon = reqIcon.GetResponse();

// Get the authentication token to use on all requests

string email = "rameshangler@gmail.com";
string password = "ramesh22";
string token = getAuthToken(email, password);

//// Example of an insert query
//string result1 = executeUpdate("insert into tableid ('column 1','column 2') values ('value 1', 'value 2')", token);

// Example of a select query, should return the newly inserted row in the previous statement
string result2 = executeSelect("select * from 203602788", token);

// Show the result in the label
//lblMessage.Text = result1 + "||" + result2;
lblMessage.Text = result2;


}/*
* Generates an authentication token by contacting ClientLogin and passing
* credentials for a given user.
*/
private string getAuthToken(String email, String password)
{
// Construct the body of the request with email, password, service, and source
string sdata =
"accountType=" + HttpUtility.UrlEncode("HOSTED_OR_GOOGLE") + "&"
+ "Email=" + HttpUtility.UrlEncode(email) + "&"
+ "Passwd=" + HttpUtility.UrlEncode(password) + "&"
+ "service=" + HttpUtility.UrlEncode("fusiontables") + "&"
+ "source=" + HttpUtility.UrlEncode("fusiontables.ApiExample")
+ "&logintoken=&logincaptcha=";

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(sdata);

// Create the request
HttpWebRequest req = (HttpWebRequest)
WebRequest.Create("https://www.google.com/accounts/ClientLogin");
if (UserName != string.Empty && Password != string.Empty)
req.Proxy.Credentials = new System.Net.NetworkCredential(UserName, Password);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = data.Length;

// Send the body of the request
Stream sout = req.GetRequestStream();
sout.Write(data, 0, data.Length);
sout.Close();

try
{
// Get the response
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader stIn = new StreamReader(res.GetResponseStream());
string s = stIn.ReadToEnd();

// Extract the Auth token component and return it
char[] delim = new char[] { '=' };
string[] parts = s.Split(delim);
return parts[3];
}
catch (WebException e)
{
// In case of error, usually 403, return the concrete error response
StreamReader stIn = new StreamReader(e.Response.GetResponseStream());
return stIn.ReadToEnd();
}
}

/*
* Executes a select query. The query will be send as a get request. Uses the
* authentication token obtained through ClientLogin. Returns the body of the
* response obtained from fusiontables.
*/
private String executeSelect(String query, String token)
{
// Encode the query in the url
string url = "http://tables.googlelabs.com/api/query?sql=" + HttpUtility.UrlEncode(query);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
if (UserName != string.Empty && Password != string.Empty)
req.Proxy.Credentials = new System.Net.NetworkCredential(UserName, Password);
// Add the authentication header
req.Headers.Add("Authorization: GoogleLogin auth=" + token);

try
{
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader stIn = new StreamReader(res.GetResponseStream());
return stIn.ReadToEnd();
}
catch (WebException e)
{
StreamReader stIn = new StreamReader(e.Response.GetResponseStream());
return stIn.ReadToEnd();
}
}

/*
* Executes an statement that modifies data, that is, insert, update, or delete.
* Uses the authentication tok
Sandeep Mewara 23-May-12 11:10am    
5!
El_Codero 23-May-12 17:33pm    
Thanks for upvoting Sandeep! @OP: Please try to set the Timeout Property of WebRequest to a higher value, but it seems your Credentials aren't valid. Go Step by Step with Debugger until exception thrown and have a look to result.
Regards
Ravi Badlia 16-Feb-15 5:54am    
which template will be used to make an application in visual studio which will fetch and send data in a fusion table?
El_Codero 16-Feb-15 9:43am    
there is no template for it. as noted by google the fusion api is build on http and json, google deployed libs for most languages. see
here

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900