using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Net;
using System.IO;
namespace TradeIdeas.TIProGUI
{
//This code is for the "sign in through google" for the desktop TIPro.
//Source inspired from Linda Lawton's site/blog: http://www.daimto.com/google-api-and-oath2/
// Lots of modification by Kathy L.
public class oAuthGoogle
{
private static string _clientId = "654054237352-39508qbhsgq1e0aj43q6g6c1lj1f8cs7.apps.googleusercontent.com";
private static string _clientSecret = "LPcPsgO1MhCkQcAf_89IJ1UQ";
private static string _redirectURI = "urn:ietf:wg:oauth:2.0:oob";
public string access_token { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
///
/// Parse the json response
/// // "{\n \"access_token\" : \"ya29.kwFUj-la2lATSkrqFlJXBqQjCIZiTg51GYpKt8Me8AJO5JWf0Sx6-0ZWmTpxJjrBrxNS_JzVw969LA\",\n \"token_type\" : \"Bearer\",\n \"expires_in\" : 3600,\n \"refresh_token\" : \"1/ejoPJIyBAhPHRXQ7pHLxJX2VfDBRz29hqS_i5DuC1cQ\"\n}"
///
///
///
public static oAuthGoogle get(string response)
{
oAuthGoogle result = new JavaScriptSerializer().Deserialize(response);
return result;
}
public static string refresh(string refreshToken) //access tokens last about an hour, so each time we process data(getting email, user's name) we'll refresh it.
{
try
{
var request = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
string postData = string.Format("client_id={0}&client_secret={1}&refresh_token={2}&grant_type=refresh_token", _clientId, _clientSecret, refreshToken);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var refreshResponse = oAuthGoogle.get(responseString);
return refreshResponse.access_token;
}
catch
{
return "";
}
}
public static oAuthGoogle Exchange(string authCode)
{
try
{
var request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/oauth2/v4/token");
string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code", authCode, _clientId, _clientSecret, _redirectURI);
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
var x = oAuthGoogle.get(responseString);
return x;
}
catch
{
return null;
}
}
public static Uri GetAutenticationURI()
{
string scopes = "email profile"; //we just want the email for now
string oauth = string.Format("https://accounts.google.com/o/oauth2/v2/auth?client_id={0}&redirect_uri={1}&scope={2}&response_type=code", _clientId, _redirectURI, scopes);
return new Uri(oauth);
}
}
}