private static GoogleMailSettingsService CreateGoogleMailService() { // Get your service account email from Google Developer's Console // Read more: https://developers.google.com/identity/protocols/OAuth2ServiceAccount const string SERVICE_ACCT_EMAIL = "<YOUR SERVICE KEY>@developer.gserviceaccount.com"; //Generate your .p12 key in the Google Developer Console and associate it with your project. var certificate = new X509Certificate2("Key.p12", "notasecret", X509KeyStorageFlags.Exportable); var serviceAccountCredentialInitializer = new ServiceAccountCredential.Initializer(SERVICE_ACCT_EMAIL) { User = "******", // A user with administrator access. Scopes = new[] { "https://apps-apis.google.com/a/feeds/emailsettings/2.0/" } }.FromCertificate(certificate); var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer); if (!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result) throw new InvalidOperationException("Access token failed."); var requestFactory = new GDataRequestFactory(null); requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken); // Replace the name of your domain and the Google Developer project you created... GoogleMailSettingsService service = new GoogleMailSettingsService("vuwall.com", "signatures"); service.RequestFactory = requestFactory; return service; }
public string GetOAuth2AccessToken(string emailAddress) { var certificate = new X509Certificate2(_certificatePath.Value, _gmailSettings.ServiceAccountCertPassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(_gmailSettings.ServiceAccountEmailAddress) { Scopes = new[] { GoogleScope.ImapAndSmtp.Name }, User = emailAddress }.FromCertificate(certificate)); var success = credential.RequestAccessTokenAsync(CancellationToken.None).Result; return success ? credential.Token.AccessToken : string.Empty; }
public static void doThatThing(Form1 f) { f.text("Querying MySQL"); var appData = getAppDataFromMySQL(); f.text("MySQL Query Complete"); string serviceAccountEmail = "*****@*****.**"; f.text("Creating X509 Cert"); var certificate = new X509Certificate2(@"C:\\Users\\Joey\\Desktop\\dmmsapi.p12", "notasecret", X509KeyStorageFlags.Exportable); f.text("Creating Google Credential"); ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) { Scopes = new[] { "https://spreadsheets.google.com/feeds" } }.FromCertificate(certificate)); credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Wait(); f.text("initializing spreadsheetservice"); SpreadsheetsService service = new SpreadsheetsService("Dalmarko-Master-Sheet"); var requestFactory = new GDataRequestFactory("Dalmarko-Master-Sheet"); requestFactory.CustomHeaders.Add(string.Format("Authorization: Bearer {0}", credential.Token.AccessToken)); service.RequestFactory = requestFactory; f.text("preparing html"); string html = "<html><table border=1><tr>"; Console.WriteLine("before ssq"); f.text("initializing spreadsheetquery"); SpreadsheetQuery query = new SpreadsheetQuery(); SpreadsheetFeed ssf = service.Query(query); foreach(SpreadsheetEntry sse in ssf.Entries) { Console.WriteLine(sse.Title.Text); WorksheetFeed wf = sse.Worksheets; foreach(WorksheetEntry we in wf.Entries) { f.text("total entries: " + we.Rows); f.max((int) we.Rows); /*AtomLink listFeedLink = we.Links.FindService(GDataSpreadsheetsNameTable.ListRel, null); ListQuery listQuery = new ListQuery(listFeedLink.HRef.ToString()); ListFeed listFeed = service.Query(listQuery); foreach(ListEntry row in listFeed.Entries) { html = html + "</tr><tr>"; foreach(ListEntry.Custom element in row.Elements) { html = html + "<td>" + element.Value + "</td>"; } }*/ int cols = (int) we.Cols; int coliterator = 0; CellQuery cq = new CellQuery(we.CellFeedLink); cq.ReturnEmpty = ReturnEmptyCells.yes; CellFeed cf = service.Query(cq); Boolean headerCellsDone = false; int rows = 1; foreach(CellEntry cell in cf.Entries) { if(!headerCellsDone) { if(cell.Value != null) { html = html + "<th>" + cell.Value + "</th>"; if(cell.Value.ToString().Contains("Parsed Sku")) { headerCellsDone = true; html = html + "</tr><tr>"; rows++; } } else { html = html + "<td></td>"; } } else { coliterator++; if(coliterator == 1) { CellQuery ncq = new CellQuery(we.CellFeedLink); ncq.MinimumRow = (uint) rows; ncq.MaximumColumn = (uint) cols; ncq.MinimumColumn = (uint) cols; try { CellFeed ncf = service.Query(ncq); string imgsrc = "http://dalmarkodesigns.com/master-images/"; CellEntry ce = (CellEntry) ncf.Entries[0]; if(ce.Value != null) imgsrc = imgsrc + ce.Value + ".jpg"; else imgsrc = imgsrc + "default.jpg"; html = html + "<td><img src='" + imgsrc + "' height=200 width=200/></td>"; } catch(Exception e) { string imgsrc = "http://dalmarkodesigns.com/master-images/default.jpg"; html = html + "<td><img src='" + imgsrc + "' height=200 width=200/></td>"; } } else { if(cell.Value != null) html = html + "<td>" + cell.Value + "</td>"; else html = html + "<td></td>"; } if(coliterator == cols) { html = html + "</tr><tr>"; rows++; f.prog(rows); f.text("processing entry " + rows + " of " + we.Rows); f.percent(rows, (int) we.Rows); coliterator = 0; } } } } } html = html + "</tr></table></html>"; File.WriteAllText("C:\\Users\\Joey\\Desktop\\index.html", html); alreadyRunning = false; }