public SheetsClient(string spreadsheetId, Authorization authorization, SheetsSerializer serializer = null) { if (serializer == null) { serializer = SheetsSerializer.Serializer; } if (serializer == null) { throw new Exception("No serializer was given."); } this.serializer = serializer; this.spreadsheetId = spreadsheetId; this.authorization = authorization; }
public static async Task <Authorization> Authorize(string key, SheetsSerializer serializer = null) { if (serializer == null) { serializer = SheetsSerializer.Serializer; } if (serializer == null) { throw new Exception("No serializer was given."); } await Task.Delay(0); Authorization authorization = new Authorization { key = key }; return(authorization); }
public static async Task <Authorization> Authorize(string clientId, string clientSecret, SheetsSerializer serializer = null) { if (serializer == null) { serializer = SheetsSerializer.Serializer; } if (serializer == null) { throw new Exception("No serializer was given."); } OAuthToken token = await OAuth.DoOAuth(clientId, clientSecret, serializer); Authorization authorization = new Authorization { accessToken = token.accessToken }; return(authorization); }
/// <summary> /// Returns the raw data that is contingent to the Google Sheets API. /// </summary> public static async Task <SpreadsheetRaw> Get(string spreadsheetId, Authorization authorization, bool includeGridData, SheetsSerializer serializer = null) { if (serializer == null) { serializer = SheetsSerializer.Serializer; } if (serializer == null) { throw new Exception("No serializer was given."); } SheetsClient client = new SheetsClient(spreadsheetId, authorization, serializer); SpreadsheetRaw raw = await client.GetRaw(includeGridData); return(raw); }
/// <summary> /// Returns a high level representation of a spreadsheet. /// </summary> public static async Task <Spreadsheet> Get(string spreadsheetId, Authorization authorization, SheetsSerializer serializer = null) { if (serializer == null) { serializer = SheetsSerializer.Serializer; } if (serializer == null) { throw new Exception("No serializer was given."); } SheetsClient client = new SheetsClient(spreadsheetId, authorization, serializer); Spreadsheet spreadsheet = await client.Get(); return(spreadsheet); }
public static async Task <OAuthToken> DoOAuth(string clientId, string clientSecret, SheetsSerializer serializer) { OAuth.serializer = serializer; // Generates state and PKCE values. string state = RandomDataBase64url(32); string codeVerifier = RandomDataBase64url(32); string codeChallenge = Base64urlencodeNoPadding(Sha256(codeVerifier)); const string codeChallengeMethod = "S256"; // Creates a redirect URI using an available port on the loopback address. string redirectURI = string.Format("http://{0}:{1}/", IPAddress.Loopback, GetRandomUnusedPort()); //Console.WriteLine("Redirect URI: " + redirectURI); // Creates an HttpListener to listen for requests on that redirect URI. HttpListener http = new HttpListener(); http.Prefixes.Add(redirectURI); //Console.WriteLine("Listening..."); http.Start(); // Creates the OAuth 2.0 authorization request. string authorizationRequest = string.Format("{0}?response_type=code&scope=openid%20profile&redirect_uri={1}&client_id={2}&state={3}&code_challenge={4}&code_challenge_method={5}", AuthorizationEndpoint, Uri.EscapeDataString(redirectURI), clientId, state, codeChallenge, codeChallengeMethod); // Opens request in the browser. System.Diagnostics.Process.Start(authorizationRequest); // Waits for the OAuth authorization response. var context = await http.GetContextAsync(); // Sends an HTTP response to the browser. HttpListenerResponse response = context.Response; string responseString = string.Format("<html><head><meta http-equiv='refresh' content='10;url=https://google.com'></head><body>Please return to the app.</body></html>"); var buffer = Encoding.UTF8.GetBytes(responseString); response.ContentLength64 = buffer.Length; var responseOutput = response.OutputStream; Task responseTask = responseOutput.WriteAsync(buffer, 0, buffer.Length).ContinueWith((task) => { responseOutput.Close(); http.Stop(); //Console.WriteLine("HTTP server stopped."); }); // Checks for errors. if (context.Request.QueryString.Get("error") != null) { string message = string.Format("OAuth authorization error: {0}.", context.Request.QueryString.Get("error")); throw new Exception(message); } if (context.Request.QueryString.Get("code") == null || context.Request.QueryString.Get("state") == null) { string message = "Malformed authorization response. " + context.Request.QueryString; throw new Exception(message); } // extracts the code string code = context.Request.QueryString.Get("code"); string incomingState = context.Request.QueryString.Get("state"); // Compares the receieved state to the expected value, to ensure that // this app made the request which resulted in authorization. if (incomingState != state) { string message = string.Format("Received request with invalid state ({0})", incomingState); throw new Exception(message); } //Console.WriteLine("Authorization code: " + code); // Starts the code exchange at the Token Endpoint. return(await PerformCodeExchange(code, codeVerifier, redirectURI, clientId, clientSecret)); }