Cybercrux

Everything is achievable through technology

Google API

POST SAMPLE

string Url = "https://accounts.google.com/o/oauth2/token";
string grant_type = "refresh_token";
string data = "refresh_token={0}&client_id={1}&client_secret={2}&grant_type={3}";
HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
string param = string.Format(data, refreshToken, _consumerKey, _consumerSecret, grant_type);
var bs = Encoding.UTF8.GetBytes(param);
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse response = request.GetResponse())
{
var sr = new StreamReader(response.GetResponseStream());
dynamic googleTokenResponce = JObject.Parse(sr.ReadToEnd());
sr.Close();
GoogleTokenData googleTokenData = googleTokenResponce.ToObject();
return googleTokenData.Access_Token;
}

GET SAMPLE

var uriBuilder = string.Format(@"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:{0}&dimensions=ga:campaign,ga:source,ga:medium&metrics=ga:users,ga:newUsers&filters=ga:medium=={1}&start-date={2}&end-date={3}&max-results=1&access_token={4}"
, profile.items.FirstOrDefault().id
, utnMedium
, startdate.ToString("yyyy-dd-MM")
, enddate.ToString("yyyy-dd-MM")
, accessToken.Token);

try
{
WebRequest forIdsWebRequest = WebRequest.Create(uriBuilder);
using (HttpWebResponse response = (HttpWebResponse)forIdsWebRequest.GetResponse())
{
using (Stream data = response.GetResponseStream())
using (var reader = new StreamReader(data))
{
dynamic tempobj = JObject.Parse(reader.ReadToEnd());
dynamic values = reader.ReadToEnd();
}
}
}
catch (Exception) { throw; }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s