示例#1
0
        /// <summary>
        /// Display the list of the the user's Google Classroom classes. Scopes
        /// requested: email, profile, and ClassroomService.Scope.ClassroomCoursesReadonly.
        /// </summary>
        public async Task <IActionResult> ListCourses(CancellationToken cancellationToken)
        {
            // Simplify the incremental auth experience by providing a login_hint. The user will
            // not be asked to select their account if they have already signed in.
            var loginHint   = GetUserEmail();
            var accessToken = await HttpContext.GetTokenAsync("ClassList", "access_token");

            var expires = await HttpContext.GetTokenAsync("ClassList", "expires_at");

            DateTime.TryParse(expires, out var expiresAt);

            if (accessToken == null || DateTime.Now > expiresAt)
            {
                return(new ChallengeResult("ClassList", new AuthenticationProperties()
                {
                    Parameters = { new KeyValuePair <string, object>("login_hint", loginHint) },
                    RedirectUri = Url.Action("ListCourses")
                }));
            }

            var model = new CoursesModel();

            try
            {
                using (var classroomService = new ClassroomService(new BaseClientService.Initializer
                {
                    HttpClientInitializer = GoogleCredential.FromAccessToken(accessToken),
                    ApplicationName = "gc2lti"
                }))
                {
                    // Get the list of the user's courses
                    model.Courses = new List <CourseModel>();

                    var coursesRequest = classroomService.Courses.List();
                    coursesRequest.CourseStates = CoursesResource.ListRequest.CourseStatesEnum.ACTIVE;
                    coursesRequest.TeacherId    = "me";

                    ListCoursesResponse coursesResponse = null;
                    do
                    {
                        if (coursesResponse != null)
                        {
                            coursesRequest.PageToken = coursesResponse.NextPageToken;
                        }

                        coursesResponse =
                            await coursesRequest.ExecuteAsync(cancellationToken).ConfigureAwait(false);

                        if (coursesResponse.Courses != null)
                        {
                            foreach (var course in coursesResponse.Courses)
                            {
                                model.Courses.Add(new CourseModel
                                {
                                    CourseId   = course.Id,
                                    CourseName = course.Name
                                });
                            }
                        }
                    } while (!string.IsNullOrEmpty(coursesResponse.NextPageToken));

                    return(View("Index", model));
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, e));
            }
        }
示例#2
0
        public static void RegisterCustomRepository(this IServiceCollection services)
        {
            services.AddTransient <IAdditionalInfoRepository, AdditionalInfoRepository>();

            services.AddTransient <IAddressRepository, AddressRepository>();

            services.AddTransient <ICommentRepository, CommentRepository>();

            services.AddTransient <IConversationRepository, ConversationRepository>();

            services.AddTransient <IFieldRepository, FieldRepository>();

            services.AddTransient <IImageRepository, ImageRepository>();

            services.AddTransient <IMediaContentRepository, MediaContentRepository>();

            services.AddTransient <IMessageRepository, MessageRepository>();

            services.AddTransient <INofticationRepository, NofticationRepository>();

            services.AddTransient <IPostRepository, PostRepository>();

            services.AddTransient <IUserRepository, UserRepository>();

            services.AddTransient <IReplyCommentRepository, ReplyCommentRepository>();

            services.AddTransient <IClientGroupRepository, ClientGroupRepository>();

            services.AddTransient <IClientGroupRepository, ClientGroupRepository>();

            services.AddTransient <ILoggingRepository, LoggingRepository>();

            services.AddTransient <IFollowRepository, FollowRepository>();

            services.AddTransient <IDownVoteRepository, DownVoteRepository>();

            services.AddTransient <IUpVoteRepository, UpVoteRepository>();

            services.AddTransient <IFcmInfoRepository, FcmInfoRepository>();

            services.AddTransient <IProvinceRepository, ProvinceRepository>();

            services.AddTransient <IDistrictRepository, DistrictRepository>();

            services.AddTransient <IWardRepository, WardRepository>();

            services.AddTransient <ILevelRepository, LevelRepository>();

            services.AddTransient <IObjectLevelRepository, ObjectLevelRepository>();

            var googleCredential = GoogleCredential.FromFile(@"wwwroot/costudy-c5390-firebase-adminsdk-e63r1-83b0a1d0a7.json");

            FirebaseApp.Create(new AppOptions()
            {
                Credential = googleCredential
            });

            services.AddTransient <IFileHelper, FileHelper>();

            services.AddTransient <IDocumentRepository, DocumentRepository>();

            services.AddTransient <IReportRepository, ReportRepository>();

            services.AddTransient <IReportReasonRepository, ReportReasonRepository>();

            services.AddTransient <INotificationDetailRepository, NotificationDetailRepository>();

            services.AddTransient <INotificationObjectRepository, NotificationObjectRepository>();

            services.AddTransient <INotificationTypeRepository, NotificationTypeRepository>();

            services.AddTransient <IConversationItemTypeRepository, ConversationItemTypeRepository>();

            services.AddTransient <IMessageImageRepository, MessageImageRepository>();

            services.AddTransient <IMessageMultiMediaRepository, MessageMultiMediaRepository>();

            services.AddTransient <IMessagePostThumbnailRepository, MessagePostThumbnailRepository>();

            services.AddTransient <IMessageTextRepository, MessageTextRepository>();

            services.AddTransient <IMessageConversationActivityRepository, MessageConversationActivityRepository>();

            services.AddTransient <IFieldGroupRepository, FieldGroupRepository>();

            services.AddTransient <IViolenceWordRepository, ViolenceWordRepository>();

            services.AddTransient <ISearchHistoryRepository, SearchHistoryRepository>();
        }
 /// <summary>
 /// Initializes an instance of the data source.
 /// </summary>
 /// <param name="projectId">The project id that contains the GCE instances to manipulate.</param>
 /// <param name="credential">The credentials to use for the call.</param>
 /// <param name="appName">The name of the application.</param>
 public GceDataSource(string projectId, GoogleCredential credential, string appName)
     : base(projectId, credential, init => new ComputeService(init), appName)
 {
 }
示例#4
0
        private void button1_Click(object sender, EventArgs e)
        {
            GoogleCredential credential;

            using (var stream = new FileStream("My First Project-2dfed0050064.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
            }

            service = new SheetsService(new Google.Apis.Services.BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            var    start           = dateTimePicker1.Value;
            var    end             = dateTimePicker2.Value;
            string numOfCategories = textBox2.Text;

            if (numOfCategories == "" && textBox1.Text == "")
            {
                MessageBox.Show(
                    "Все поля должны быть заполнены",
                    "Ошибка",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button1,
                    MessageBoxOptions.ServiceNotification);
                return;
            }

            var           valueRange = new ValueRange();
            logic         g          = new logic();
            List <string> dates      = new List <string>();

            g.initializeDates(start, end, ref dates);
            char ch = 'G';
            int  x  = (int)ch + dates.Count;

            ch = (char)x;
            var range1 = "Main Table!A:" + ch;

            var objectList2 = new List <object>();

            objectList2.Add("Гостиница");
            objectList2.Add("Кол-во чел.");
            objectList2.Add("ФИО");
            objectList2.Add("Номер");
            objectList2.Add("Заезд");
            objectList2.Add("Выезд");


            foreach (var i in dates)
            {
                objectList2.Add(i);
            }
            valueRange.Values = new List <IList <object> > {
                objectList2
            };
            var appendRequest2 = service.Spreadsheets.Values.Append(valueRange, SpreadsheetId, range1);

            appendRequest2.ValueInputOption = SpreadsheetsResource.ValuesResource.AppendRequest.ValueInputOptionEnum.USERENTERED;
            var appendResponse2 = appendRequest2.Execute();

            var range3      = "Data Table!D1" + ":" + ch;
            var objectList3 = new List <object>();

            objectList3.Add("Блок в отеле");
            objectList3.Add("");
            objectList3.Add("");
            foreach (var i in dates)
            {
                objectList3.Add(i);
            }
            g.updateRequest(range3, objectList3);

            var range4      = "Data Table!D" + (3 + int.Parse(numOfCategories)) + ":" + ch;
            var objectList4 = new List <object>();

            objectList4.Add("Фактический блок");
            g.updateRequest(range4, objectList4);

            var range5      = "Data Table!D" + (5 + int.Parse(numOfCategories) * 2) + ":" + ch;
            var objectList5 = new List <object>();

            objectList5.Add("Разница");
            g.updateRequest(range5, objectList5);
        }
示例#5
0
        public static void DeleteObjectGStorage(string bucketName, IEnumerable <string> objectNames, GoogleCredential credential)
        {
            var storage = StorageClient.Create(credential);

            foreach (string objectName in objectNames)
            {
                storage.DeleteObject(bucketName, objectName);
                Console.WriteLine($"Deleted {objectName}.");
            }
        }
示例#6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDataAccessLibrary()
            .AddSqlDatabase(() => new MySqlConnection(Configuration.GetConnectionString("Default")))
            .AddBCryptPasswordHash()
            .AddGoogleCloudStorage(GoogleCredential.FromFile(Configuration.GetSection("CloudStorage").GetSection("StorageCredential").Value), options => { });

            var tokenProvider = new RsaTokenProvider("api.tobymeehan.com", "api.tobymeehan.com", Guid.NewGuid().ToString());

            services.AddSingleton <ITokenProvider>(tokenProvider);

            services.AddSingleton(ConfigureMapper());

            services.AddControllersWithViews();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = false;
                options.TokenValidationParameters = tokenProvider.GetValidationParameters();
            });

            var    keyRingConfig        = Configuration.GetSection("KeyRing");
            string keyRingBucket        = keyRingConfig.GetSection("BucketName").Value;
            string dataProtectionObject = keyRingConfig.GetSection("DataProtection").Value;

            services.AddSharedCookieAuthentication(keyRingBucket, dataProtectionObject, options =>
            {
                Func <HttpContext, string> getReturnUrl = context => $"?ReturnUrl={WebUtility.UrlEncode($"{context.Request.Host}{context.Request.Path}")}";

                options.Events = new CookieAuthenticationEvents
                {
                    OnRedirectToLogin = context =>
                    {
#if DEBUG
                        context.HttpContext.Response.Redirect($"https://localhost:44373/login{getReturnUrl.Invoke(context.HttpContext)}");
#else
                        context.HttpContext.Response.Redirect($"https://tobymeehan.com/login{getReturnUrl.Invoke(context.HttpContext)}");
#endif
                        return(Task.CompletedTask);
                    },
                    OnRedirectToAccessDenied = context =>
                    {
#if DEBUG
                        context.HttpContext.Response.Redirect($"https://localhost:44373/login{getReturnUrl.Invoke(context.HttpContext)}");
#else
                        context.HttpContext.Response.Redirect($"https://tobymeehan.com/login{getReturnUrl.Invoke(context.HttpContext)}");
#endif
                        return(Task.CompletedTask);
                    }
                };
            });

            services.AddScopeAuthorization();

            services.AddAuthorization(options =>
            {
                AuthorizationPolicy jwt = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme)
                                          .RequireAuthenticatedUser()
                                          .Build();

                AuthorizationPolicy cookies = new AuthorizationPolicyBuilder(CookieAuthenticationDefaults.AuthenticationScheme)
                                              .RequireAuthenticatedUser()
                                              .Build();

                options.AddPolicy("jwt", jwt);
                options.AddPolicy("cookies", cookies);

                options.DefaultPolicy = jwt;
            });

            services.AddSingleton <IAuthorizationHandler, UserAuthorizationHandler>();
        }
示例#7
0
 public GoogleCloudStorage(IConfiguration configuration)
 {
     googleCredential = GoogleCredential.FromFile(configuration.GetValue <string>("GoogleCredentialFile"));
     storageClient    = StorageClient.Create(googleCredential);
     bucketName       = configuration.GetValue <string>("GoogleCloudStorageBucket");
 }
示例#8
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();


            services.AddControllers().AddNewtonsoftJson(options =>
                                                        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);

            Firebase fbconfig = new Firebase();

            Configuration.Bind("Firebase", fbconfig);

            var json = JsonConvert.SerializeObject(fbconfig);

            FirebaseApp.Create(new AppOptions()
            {
                Credential = GoogleCredential.FromJson(json),
            });

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.Authority = "https://securetoken.google.com/danceschool-45356";
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer   = true,
                    ValidIssuer      = "https://securetoken.google.com/danceschool-45356",
                    ValidateAudience = true,
                    ValidAudience    = "danceschool-45356",
                    ValidateLifetime = true
                };
            });

            services.AddMvc(option =>
            {
                option.EnableEndpointRouting = false;
                option.Filters.Add(typeof(ModelStateFeatureFilter));
            });
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "./Client/build";
            });

            services.AddRazorPages();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Dance School", Version = "v1"
                });
            });

            services.AddSwaggerGen(c =>
            {
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey,
                    Scheme      = "bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement {
                    {
                        new OpenApiSecurityScheme {
                            Reference = new OpenApiReference {
                                Id   = "Bearer", //The name of the previously defined security scheme.
                                Type = ReferenceType.SecurityScheme
                            }
                        }, new List <string>()
                    }
                });

                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title   = "Dance school API",
                    Version = "v1",
                    Contact = new OpenApiContact
                    {
                        Name  = "Zoe Zhang",
                        Email = "*****@*****.**",
                        Url   = new Uri("https://github.com/Zoe-0925/"),
                    }
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                c.IncludeXmlComments(xmlPath);
            });

            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = Configuration.GetConnectionString("RedisAzure");
                options.InstanceName  = "master";
            });

            services.AddDbContext <ApplicationContext>(options =>
                                                       options.UseSqlServer(
                                                           Configuration.GetConnectionString("Production"),
                                                           b => b.MigrationsAssembly(typeof(ApplicationContext).Assembly.FullName)));

            services.AddMediatR(typeof(Startup));

            services.AddAutoMapper(typeof(Startup));
        }
示例#9
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string credFile, filePath, csvFile, fromDate, toDate;

            fromDate = Request.QueryString["fromDate"];
            toDate   = Request.QueryString["toDate"];

            if (string.IsNullOrEmpty(fromDate) || string.IsNullOrEmpty(toDate))
            {
                Response.Write("Date range not specified. Must be in format yyyy-mm-dd");
            }
            else
            {
                if (Request.IsLocal)
                {
                    filePath = "C:\\Projects\\lyonsplace\\website\\FrontEnd\\App_Data\\";
                    credFile = "bryanston.json";
                }
                else
                {
                    filePath = "D:\\websites\\lyonsplace\\live\\App_Data\\";
                    credFile = "lyonsplace.json";
                }

                csvFile = DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv";

                try
                {
                    // These are the scopes of permissions you need. It is best to request only what you need and not all of them
                    string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics };                                 // View your Google Analytics data

                    GoogleCredential credential;
                    using (var stream = new FileStream(filePath + credFile, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(scopes);
                    }

                    // Create the Analytics service.
                    AnalyticsReportingService svc = new AnalyticsReportingService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName       = "Almacantar",
                    });

                    var dateRange = new DateRange
                    {
                        StartDate = fromDate,
                        EndDate   = toDate
                    };
                    var metric = new Metric
                    {
                        Expression = "ga:pageviews",
                        Alias      = "Sessions"
                    };

                    List <Dimension> dimensions = new List <Dimension>();
                    dimensions.Add(new Dimension {
                        Name = "ga:source"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:medium"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:adContent"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:userAgeBracket"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:userGender"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:country"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:language"
                    });
                    dimensions.Add(new Dimension {
                        Name = "ga:userType"
                    });

                    var reportRequest = new ReportRequest
                    {
                        Dimensions = dimensions,
                        Metrics    = new List <Metric> {
                            metric
                        },
                        ViewId = "150454507"                         //lyons place = 183611330, almacantar = "150454507"
                    };

                    var getReportsRequest = new GetReportsRequest
                    {
                        ReportRequests = new List <ReportRequest> {
                            reportRequest
                        }
                    };

                    var batchRequest = svc.Reports.BatchGet(getReportsRequest);
                    var response     = batchRequest.Execute();

                    StringBuilder sb = new StringBuilder();

                    // csv header
                    sb.Append("source,medium,adContent,age,gender,country,language,userType,pageViews" + Environment.NewLine);

                    foreach (var x in response.Reports.First().Data.Rows)
                    {
                        sb.Append(string.Join(",", x.Dimensions) + "," + string.Join(", ", x.Metrics.First().Values) + "<br/>");
                    }

                    Response.Write(sb.ToString());

                    #region write to file

                    /*
                     * StreamWriter sw = File.CreateText(filePath + csvFile);
                     * sw.WriteLine(sb.ToString());
                     * sw.Close();
                     *
                     * Response.ContentType = "text/csv";
                     * Response.AppendHeader("Content-Disposition", "attachment; filename=" + csvFile);
                     * Response.TransmitFile(filePath + csvFile);
                     * Response.End();
                     */
                    #endregion
                }
                catch (Exception ex)
                {
                }
            }
        }
示例#10
0
        private async Task Run()
        {
            GoogleCredential creds;

            using (var stream = new FileStream(_credsFilePath, FileMode.Open))
            {
                creds = GoogleCredential.FromStream(stream).CreateScoped(
                    AndroidPublisherService.Scope.Androidpublisher);
            }

            var service = new AndroidPublisherService(new BaseClientService.Initializer
            {
                HttpClientInitializer = creds
            });

            service.HttpClient.Timeout = TimeSpan.FromMinutes(3);

            var editRequest = service.Edits.Insert(null, Package);
            var edit        = await editRequest.ExecuteAsync();

            Console.WriteLine("Created edit with id {0}.", edit.Id);

            Bundle aab = null;

            using (var stream = new FileStream(_aabFilePath, FileMode.Open))
            {
                var uploadMedia = service.Edits.Bundles.Upload(Package, edit.Id, stream,
                                                               "application/octet-stream");

                var progress = await uploadMedia.UploadAsync();

                if (progress.Status == Google.Apis.Upload.UploadStatus.Completed)
                {
                    aab = uploadMedia.ResponseBody;
                }
                else
                {
                    if (progress.Exception != null)
                    {
                        Console.WriteLine("Upload exception: {0}", progress.Exception);
                    }
                    // throw new Exception("Upload failed.");
                }
            }

            // Console.WriteLine("Version code {0} has been uploaded.", aab.VersionCode);
            //
            // var trackRequest = service.Edits.Tracks.Update(new Track
            // {
            //     TrackValue = _track,
            //     Releases = new List<TrackRelease>
            //     {
            //         new TrackRelease { VersionCodes = new List<long?> { aab.VersionCode }, Status = "completed" }
            //     }
            // }, Package, edit.Id, _track);
            //
            // var updatedTrack = await trackRequest.ExecuteAsync();
            // Console.WriteLine("Track {0} has been updated.", updatedTrack.TrackValue);
            //
            // var commitRequest = service.Edits.Commit(Package, edit.Id);
            // var commitEdit = await commitRequest.ExecuteAsync();
            // Console.WriteLine("App edit with id {0} has been comitted.", commitEdit.Id);
        }
示例#11
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="jsonPath">jsonファイルのパス</param>
 /// <param name="viewId">ビューID</param>
 public GA(string jsonPath, string viewId)
 {
     this.jsonPath   = jsonPath;
     this.viewId     = viewId;
     this.credential = GetCredential();
 }
示例#12
0
 /// <summary>
 /// Provided for dependency injection, this will use default application credentials.
 /// </summary>
 public BigtableCredentials()
 {
     // This is totally cheating
     _googleCredentials = UseApplicationDefaultCredentialsAsync().Result._googleCredentials;
 }
示例#13
0
 /// <summary>
 /// Scopes can be used
 /// </summary>
 public BigtableCredentials(GoogleCredential googleCredentials)
 {
     _googleCredentials = googleCredentials;
 }
        public static List <SpotifyInfo> GetAllSongsFromStaticSheet()
        {
            try
            {
                ServiceAccountCredential credential1;
                string[] Scopes = { SheetsService.Scope.Spreadsheets };
                string   serviceAccountEmail = "*****@*****.**";
                string   jsonfile            = "trackingNewData.json";
                string   spreadsheetID       = "1XsrVqD-Fz1ggj2VX6wEbpt_FO0qguTMJR5YWnytYXV4";
                string   range = "All";
                using (Stream stream = new FileStream(@jsonfile, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    credential1 = (ServiceAccountCredential)
                                  GoogleCredential.FromStream(stream).UnderlyingCredential;

                    var initializer = new ServiceAccountCredential.Initializer(credential1.Id)
                    {
                        User   = serviceAccountEmail,
                        Key    = credential1.Key,
                        Scopes = Scopes
                    };
                    credential1 = new ServiceAccountCredential(initializer);
                }
                var serices = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential1,
                    ApplicationName       = ApplicationName,
                });
                SpreadsheetsResource.ValuesResource.GetRequest.ValueRenderOptionEnum    valueRenderOption    = (SpreadsheetsResource.ValuesResource.GetRequest.ValueRenderOptionEnum) 0;
                SpreadsheetsResource.ValuesResource.GetRequest.DateTimeRenderOptionEnum dateTimeRenderOption = (SpreadsheetsResource.ValuesResource.GetRequest.DateTimeRenderOptionEnum) 0;

                SpreadsheetsResource.ValuesResource.GetRequest request = serices.Spreadsheets.Values.Get(spreadsheetID, range);
                request.ValueRenderOption    = valueRenderOption;
                request.DateTimeRenderOption = dateTimeRenderOption;

                // To execute asynchronously in an async method, replace `request.Execute()` as shown:
                Data.ValueRange         response = request.Execute();
                IList <IList <Object> > values   = response.Values;
                values.RemoveAt(0);
                List <SpotifyInfo> listSongs = new List <SpotifyInfo>();
                foreach (var item in values)
                {
                    if (item.Count != 0)
                    {
                        SpotifyInfo song = new SpotifyInfo();
                        for (int i = 0; i < item.Count; i++)
                        {
                            if (i == 1)
                            {
                                if (item[i] != null)
                                {
                                    song.TrackTitle = item[i].ToString();
                                }
                            }
                            else if (i == 2)
                            {
                                song.Code = item[i].ToString();
                            }
                            else if (i == 3)
                            {
                                if (item[i] != null)
                                {
                                    song.Artists = item[i].ToString();
                                }
                            }
                            else if (i == 4)
                            {
                                if (!string.IsNullOrEmpty(item[i].ToString()))
                                {
                                    song.LinkSpotify = item[i].ToString();
                                    song.TrackId     = item[i].ToString().Split(new string[] { "=" }, StringSplitOptions.None)[1].Split(new string[] { ":" }, StringSplitOptions.None)[2];
                                    song.AlbumId     = item[i].ToString().Split(new string[] { "?" }, StringSplitOptions.None)[0].Split(new string[] { "https://open.spotify.com/album/" }, StringSplitOptions.None)[1];
                                }
                            }
                            else if (i == 5)
                            {
                                if (item[i] != null)
                                {
                                    song.Genres = item[i].ToString();
                                }
                            }
                            else if (i == 6)
                            {
                                if (item[i] != null)
                                {
                                    song.Country = item[i].ToString();
                                }
                            }
                            else if (i == 7)
                            {
                                if (item[i] != null)
                                {
                                    song.ReleaseDate = item[i].ToString();
                                }
                            }
                            else if (i == 8)
                            {
                                if (item[i] != null)
                                {
                                    song.Popularity = item[i].ToString();
                                }
                            }
                            else if (i == 9)
                            {
                                if (item[i] != null)
                                {
                                    if (item[i].ToString() != "")
                                    {
                                        song.StreamCount = long.Parse(item[i].ToString());
                                    }
                                }
                            }
                        }
                        listSongs.Add(song);
                    }
                }
                return(listSongs);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
        /// <summary>
        /// Asynchronously creates a <see cref="TranslationClient"/>, using application default credentials if
        /// no credentials are specified.
        /// </summary>
        /// <remarks>
        /// The credentials are scoped as necessary.
        /// </remarks>
        /// <param name="credential">Optional <see cref="GoogleCredential"/>.</param>
        /// <param name="model">The default translation model to use. Defaults to <see cref="TranslationModel.ServiceDefault"/>.</param>
        /// <returns>The task representing the created <see cref="TranslationClient"/>.</returns>
        public static async Task <TranslationClient> CreateAsync(GoogleCredential credential = null, TranslationModel model = TranslationModel.ServiceDefault)
        {
            var scopedCredentials = await _credentialProvider.GetCredentialsAsync(credential).ConfigureAwait(false);

            return(CreateImpl(scopedCredentials, null, model));
        }
示例#16
0
        static void Main(string[] args)
        {
            //refer to https://developers.google.com/drive/api/v3/quickstart/dotnet to get this json file
            string           credPath = @"c:\orbit-y-drive-service.json";
            GoogleCredential credential;

            using (var stream = new FileStream(credPath, FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes);
            }

            DriveService service = new DriveService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = ApplicationName,
            });

            string teamDrivePageToken = null;
            string teamDriveID        = null;

            do
            {
                TeamdrivesResource.ListRequest teamDriveList = service.Teamdrives.List();
                teamDriveList.Fields    = "nextPageToken, teamDrives(kind, id, name)";
                teamDriveList.PageToken = teamDrivePageToken;
                var result     = teamDriveList.Execute();
                var teamDrives = result.TeamDrives;

                if (teamDrives != null && teamDrives.Count > 0)
                {
                    foreach (TeamDrive drive in teamDrives)
                    {
                        if (drive.Name == TeamDriveName)
                        {
                            teamDriveID = drive.Id;
                            break;
                        }
                    }
                }
                teamDrivePageToken = result.NextPageToken;
            } while (teamDrivePageToken != null && teamDriveID == null);

            if (teamDriveID == null)
            {
                WriteLogEntry("Team drive not found", null);
                StopService();
                return;
            }

            string rootFolderPageToken = null;
            string rootFolderId        = "";

            do
            {
                FilesResource.ListRequest rootFolderRequest = service.Files.List();
                rootFolderRequest.Fields                = "nextPageToken, files(id, name, parents, mimeType)";
                rootFolderRequest.PageToken             = rootFolderPageToken;
                rootFolderRequest.SupportsTeamDrives    = true;
                rootFolderRequest.IncludeTeamDriveItems = true;
                rootFolderRequest.Corpora               = "teamDrive";
                rootFolderRequest.TeamDriveId           = teamDriveID;
                rootFolderRequest.Q = "parents='" + teamDriveID + "' and trashed=false and name='" + RootFolderName + "'";

                var result = rootFolderRequest.Execute();
                var files  = result.Files;
                if (files != null && files.Count == 1)
                {
                    var file = files[0];
                    if (file.MimeType == "application/vnd.google-apps.folder")
                    {
                        rootFolderId = file.Id;
                        break;
                    }
                }
                rootFolderPageToken = result.NextPageToken;
            } while (rootFolderPageToken != null && rootFolderId == null);

            if (rootFolderId == "")
            {
                WriteLogEntry("Error finding Root Folder. This can be caused by a duplicate folder, no folder, or execution error etc.", null);
                StopService();
                return;
            }

            string recordFolderPageToken = null;
            List <Google.Apis.Drive.v3.Data.File> recordFolders = new List <Google.Apis.Drive.v3.Data.File>();

            do
            {
                FilesResource.ListRequest recordFoldersRequest = service.Files.List();
                recordFoldersRequest.Fields                = "nextPageToken, files(id, name, parents, mimeType)";
                recordFoldersRequest.PageToken             = recordFolderPageToken;
                recordFoldersRequest.SupportsTeamDrives    = true;
                recordFoldersRequest.IncludeTeamDriveItems = true;
                recordFoldersRequest.Corpora               = "teamDrive";
                recordFoldersRequest.TeamDriveId           = teamDriveID;
                recordFoldersRequest.Q = "parents='" + rootFolderId + "' and trashed=false";

                var result = recordFoldersRequest.Execute();
                var files  = result.Files;

                if (files != null && files.Count > 0)
                {
                    foreach (Google.Apis.Drive.v3.Data.File file in files)
                    {
                        if (file.MimeType == "application/vnd.google-apps.folder")
                        {
                            recordFolders.Add(file);
                        }
                        else
                        {
                            //file found. shouldnt happen - delete
                            TrashDriveObject(service, file.Id);
                        }
                    }
                }
                recordFolderPageToken = result.NextPageToken;
            } while (recordFolderPageToken != null);

            if (recordFolders.Count != 0)
            {
                foreach (Google.Apis.Drive.v3.Data.File recordFolder in recordFolders)
                {
                    string addOnEmail = "";
                    if (isAddOnFinished(service, teamDriveID, recordFolder.Id, ref addOnEmail))
                    {
                        string recordNumber = recordFolder.Name;
                        if (isValidRecordNumber(recordNumber) == false && isValidSubawardNumber(recordNumber) == false)
                        {
                            SendErrorEmail(addOnEmail, "Record number " + recordNumber + " does not exist in ORBiT.");
                            TrashDriveObject(service, recordFolder.Id);
                            continue;
                        }

                        string EFilesFolder = GetEFilesFolder(recordNumber);
                        if (EFilesFolder == "")
                        {
                            SendErrorEmail(addOnEmail, "Could not find an EFiles folder. Record number is " + recordNumber);
                            TrashDriveObject(service, recordFolder.Id);
                            continue;
                        }

                        string        filePageToken = null;
                        List <string> lstSavedFiles = new List <string>();
                        do
                        {
                            FilesResource.ListRequest filesRequest = service.Files.List();
                            filesRequest.Fields                = "nextPageToken, files(id, name, parents, mimeType)";
                            filesRequest.PageToken             = filePageToken;
                            filesRequest.SupportsTeamDrives    = true;
                            filesRequest.IncludeTeamDriveItems = true;
                            filesRequest.Corpora               = "teamDrive";
                            filesRequest.TeamDriveId           = teamDriveID;
                            filesRequest.Q = "parents='" + recordFolder.Id + "' and trashed=false";

                            var  result   = filesRequest.Execute();
                            var  files    = result.Files;
                            bool stopFlag = false;

                            foreach (Google.Apis.Drive.v3.Data.File file in files)
                            {
                                var fileInfo = service.Files.Get(file.Id);
                                if (file.MimeType == "application/vnd.google-apps.folder")
                                {
                                    //folder found - shouldnt happen - trash folder
                                    TrashDriveObject(service, file.Id);
                                }
                                else
                                {
                                    if (file.Name == FlagFileName || file.Name == TempFileName)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        using (MemoryStream ms = new MemoryStream())
                                        {
                                            fileInfo.Download(ms);
                                            string filenameToSave = getUniqueFilename(EFilesFolder, file.Name);
                                            try
                                            {
                                                using (FileStream fs = new FileStream(EFilesFolder + filenameToSave, FileMode.Create, System.IO.FileAccess.Write))
                                                {
                                                    try
                                                    {
                                                        fs.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
                                                    }
                                                    catch (Exception e)
                                                    {
                                                        string message = "Error saving file to Y Drive. Record Number: " + recordNumber + ", file name: " +
                                                                         file.Name;
                                                        WriteLogEntry(message, e);

                                                        if (lstSavedFiles.Count > 0)
                                                        {
                                                            message += "<br /><br />";
                                                            message += "The following files were saved successfully:<br /><ul>";
                                                            for (int i = 0; i < lstSavedFiles.Count; i++)
                                                            {
                                                                message += "<li>" + lstSavedFiles[i] + "</li>";
                                                            }
                                                            message += "</ul>";
                                                        }

                                                        SendErrorEmail(addOnEmail, message);

                                                        stopFlag = true;
                                                        ms.Close();
                                                        break;
                                                    }

                                                    lstSavedFiles.Add(filenameToSave);
                                                }
                                                ms.Close();
                                            }
                                            catch (Exception e)
                                            {
                                                WriteLogEntry("Error writing to filestream. File name is " + filenameToSave, e);

                                                string message = "Error saving file to Y Drive. Record Number: " + recordNumber + ", file name: " +
                                                                 file.Name;
                                                if (lstSavedFiles.Count > 0)
                                                {
                                                    message += "<br /><br />";
                                                    message += "The following files were saved successfully:<br /><ul>";
                                                    for (int i = 0; i < lstSavedFiles.Count; i++)
                                                    {
                                                        message += "<li>" + lstSavedFiles[i] + "</li>";
                                                    }
                                                    message += "</ul>";
                                                }

                                                SendErrorEmail(addOnEmail, message);

                                                stopFlag = true;
                                                break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (stopFlag == true)
                            {
                                break;
                            }

                            filePageToken = result.NextPageToken;
                        } while (filePageToken != null);

                        TrashDriveObject(service, recordFolder.Id);
                    }
                }
            }
        }
        /// <summary>
        /// Synchronously creates a <see cref="TranslationClient"/>, using application default credentials if
        /// no credentials are specified.
        /// </summary>
        /// <remarks>
        /// The credentials are scoped as necessary.
        /// </remarks>
        /// <param name="credential">Optional <see cref="GoogleCredential"/>.</param>
        /// <param name="model">The default translation model to use. Defaults to <see cref="TranslationModel.ServiceDefault"/>.</param>
        /// <returns>The created <see cref="TranslationClient"/>.</returns>
        public static TranslationClient Create(GoogleCredential credential = null, TranslationModel model = TranslationModel.ServiceDefault)
        {
            var scopedCredentials = _credentialProvider.GetCredentials(credential);

            return(CreateImpl(scopedCredentials, null, model));
        }
        private UrlSigner CreateUrlSigner(Options options)
        {
            var credential = GoogleCredential.FromFile(options.GCPServiceCredentialFilePath);

            return(UrlSigner.FromServiceAccountCredential(credential.UnderlyingCredential as ServiceAccountCredential));
        }
示例#19
0
        /// <summary>
        ///
        /// <para>BVMServiceGC: Parametered Constructor for Managed Service by Google</para>
        ///
        /// <para>Parameters:</para>
        /// <para><paramref name="_ProgramUniqueID"/>           Program Unique ID</para>
        /// <para><paramref name="_ProjectID"/>                 GC Project ID</para>
        /// <para><paramref name="_ZoneName"/>                  GC Compute Engine Zone Name</para>
        /// <para><paramref name="_ErrorMessageAction"/>        Error messages will be pushed to this action</para>
        ///
        /// </summary>
        public BVMServiceGC(
            string _ProgramUniqueID,
            string _ProjectID,
            string _ZoneName,
            Action <string> _ErrorMessageAction = null)
        {
            ProgramUniqueID = _ProgramUniqueID;
            ProjectID       = _ProjectID;
            ZoneName        = _ZoneName;
            try
            {
                string ApplicationCredentials      = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
                string ApplicationCredentialsPlain = Environment.GetEnvironmentVariable("GOOGLE_PLAIN_CREDENTIALS");
                if (ApplicationCredentials == null && ApplicationCredentialsPlain == null)
                {
                    _ErrorMessageAction?.Invoke("BVMServiceGC->Constructor: GOOGLE_APPLICATION_CREDENTIALS (or GOOGLE_PLAIN_CREDENTIALS) environment variable is not defined.");
                    bInitializationSucceed = false;
                }
                else
                {
                    if (ApplicationCredentials == null)
                    {
                        if (!BUtility.HexDecode(out ApplicationCredentialsPlain, ApplicationCredentialsPlain, _ErrorMessageAction))
                        {
                            throw new Exception("Hex decode operation for application credentials plain has failed.");
                        }
                        Credential = GoogleCredential.FromJson(ApplicationCredentialsPlain)
                                     .CreateScoped(
                            new string[]
                        {
                            ComputeService.Scope.Compute,
                            ComputeService.Scope.CloudPlatform
                        })
                                     .UnderlyingCredential as ServiceAccountCredential;
                    }
                    else
                    {
                        using (var Stream = new FileStream(ApplicationCredentials, FileMode.Open, FileAccess.Read))
                        {
                            Credential = GoogleCredential.FromStream(Stream)
                                         .CreateScoped(
                                new string[]
                            {
                                ComputeService.Scope.Compute,
                                ComputeService.Scope.CloudPlatform
                            })
                                         .UnderlyingCredential as ServiceAccountCredential;
                        }
                    }

                    if (Credential != null)
                    {
                        bInitializationSucceed = true;
                    }
                    else
                    {
                        bInitializationSucceed = false;
                    }
                }
            }
            catch (Exception e)
            {
                _ErrorMessageAction?.Invoke("BVMServiceGC->Constructor: " + e.Message + ", Trace: " + e.StackTrace);
                bInitializationSucceed = false;
            }
        }
示例#20
0
        public KmsDataProtectionProvider(IOptions <KmsDataProtectionProviderOptions> options)
        {
            _options = options;

            GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;

            if (credential.IsCreateScopedRequired)
            {
                credential = credential.CreateScoped(new[] { CloudKMSService.Scope.CloudPlatform });
            }

            _kms = new CloudKMSService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                GZipEnabled           = false
            });

            var parent = string.Format("projects/{0}/locations/{1}", options.Value.ProjectId, options.Value.Location);

            KeyRing keyRingToCreate = new KeyRing();

            var request = new ProjectsResource.LocationsResource.KeyRingsResource.CreateRequest(_kms, keyRingToCreate, parent);

            request.KeyRingId = options.Value.KeyRing;

            try
            {
                request.Execute();
            }
            catch (Google.GoogleApiException e)
                when(e.HttpStatusCode == System.Net.HttpStatusCode.Conflict) /* Already exists.  Ok.*/ }
            {

        }

        IDataProtector IDataProtectionProvider.CreateProtector(string purpose)
        {
            IDataProtector cached;

            if (_dataProtectorCache.TryGetValue(purpose, out cached))
            {
                return(cached);
            }

            var keyRingName = string.Format(
                "projects/{0}/locations/{1}/keyRings/{2}",
                _options.Value.ProjectId, _options.Value.Location,
                _options.Value.KeyRing);

            string rotationPeriod = string.Format("{0}s", TimeSpan.FromDays(7).TotalSeconds);

            CryptoKey cryptoKeyToCreate = new CryptoKey()
            {
                Purpose          = "ENCRYPT_DECRYPT",
                NextRotationTime = DateTime.UtcNow.AddDays(7),
                RotationPeriod   = rotationPeriod
            };

            var request = new ProjectsResource.LocationsResource.KeyRingsResource.CryptoKeysResource.CreateRequest(_kms, cryptoKeyToCreate, keyRingName);

            string keyId = EscapeKeyId(purpose);

            request.CryptoKeyId = keyId;

            string keyName;

            try
            {
                keyName = request.Execute().Name;
            }
            catch (Google.GoogleApiException e)
                when(e.HttpStatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    // Already exists.  Ok.
                    keyName = string.Format("{0}/cryptoKeys/{1}", keyRingName, keyId);
                }

            var newProtector = new KmsDataProtector(_kms, keyName, (string innerPurpose) => this.CreateProtector($"{purpose}.{innerPurpose}"));

            _dataProtectorCache.TryAdd(purpose, newProtector);

            return(newProtector);
        }
示例#21
0
        static void Main(string[] args)
        {
            // ************************************************************
            // ASSUMPTION: Unit test has been run on Google Drive first
            // ************************************************************

            var googleDriveManager = new GoogleDriveManager();
            var googleSheetManager = new GoogleSheetManager();

            GoogleCredential credentialReadOnly   = null;
            GoogleCredential credentialFullAccess = null;

            DriveService driveServiceReadOnly   = null;
            DriveService driveServiceFullAccess = null;

            SheetsService sheetsServiceReadOnly   = null;
            SheetsService sheetsServiceFullAccess = null;

            #region STEP 0: Pre-test validation

            if (System.IO.File.Exists("client-secret.json") == false)
            {
                Console.WriteLine("******* ERROR: Please ensure you have downloaded your Google API .JSON file and copied into same folder as the .EXE *******");
                Console.ReadKey();
                return;
            }

            // Get the readonly drive credentials. (If it fails here run UnitTestGoogleDriveViaConsole to troubleshoot)
            credentialReadOnly = googleDriveManager.GetUserCredential("client-secret.json", googleDriveManager.ReadOnlyScope);
            if (credentialReadOnly == null)
            {
                Console.WriteLine("******* PRE-TEST VALIDATION FAILED - Credentials returned null when getting Read Only Scope permissions");
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            // Get the full access drive credentials. (If it fails here run UnitTestGoogleDriveViaConsole to troubleshoot)
            credentialFullAccess = googleDriveManager.GetUserCredential("client-secret.json", googleDriveManager.FullAccessScope);
            if (credentialFullAccess == null)
            {
                Console.WriteLine("******* PRE-TEST VALIDATION FAILED - Credentials returned null when getting Full Access Scope permissions");
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            // Get Drive Service Instance as Read-Only
            driveServiceReadOnly = googleDriveManager.CreateDriveServiceInstance(credentialReadOnly, "UnitTest Google Sheet Library");
            if (driveServiceReadOnly == null)
            {
                Console.WriteLine("******* STEP 2.2.1 - FAILED - drive service returned null getting it with Read Only credentials. ");
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            // Get Drive Service Instance as Full Access
            driveServiceFullAccess = googleDriveManager.CreateDriveServiceInstance(credentialFullAccess, "UnitTest Google Sheet Library");
            if (driveServiceFullAccess == null)
            {
                Console.WriteLine("******* STEP 2.2.2 - FAILED - drive service returned null getting it with Full Access credentials. ");
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            var t1Folder = googleDriveManager.GetFolderByName(driveServiceReadOnly, "T1");
            if (t1Folder == null)
            {
                Console.WriteLine("******* ERROR: T1 folder should already exist. Please ensure you ran UnitTestGoogleDriveViaConsole first. *******");
                Console.ReadKey();
                return;
            }

            // Check if sheet exists and so delete it
            var sheetFile = googleDriveManager.GetSheetFileInChildFolderByName(driveServiceReadOnly, "TestSheet", t1Folder.Id);
            if (sheetFile != null)
            {
                googleDriveManager.DeleteFile(driveServiceFullAccess, sheetFile.Id);
            }

            #endregion

            #region STEP 1: Create the test sheet

            #region STEP 1.1: Negative Testing

            // STEP 1.1.1 - Test no drive service
            try { var expectedFailure = googleDriveManager.CreateGoogleSheet(null, string.Empty, string.Empty, null); }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 1.1.1 - PASSED : EXPECTED FAILURE with no drive service passed: ");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
            }

            // STEP 3.1.2 - Test drive service, no sheet name
            try { var expectedFailure = googleDriveManager.CreateGoogleSheet(driveServiceFullAccess, string.Empty, string.Empty, new List <string> {
                    t1Folder.Id
                }); }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 3.1.2 - PASSED : EXPECTED FAILURE with drive service but no sheet name: ");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
            }

            #endregion

            #region STEP 1.2 Positive Testing

            var sheetFileId = googleDriveManager.CreateGoogleSheet(driveServiceFullAccess, "TestSheet", "Sheet used to Unit test CSHARP Sheet Library.", new List <string> {
                t1Folder.Id
            });
            if (string.IsNullOrEmpty(sheetFileId))
            {
                Console.WriteLine("******* ERROR: Failed to create test sheet *******");
                Console.ReadKey();
                return;
            }

            #endregion

            #endregion

            #region STEP 2: Assign Read Permissions to [email protected]

            // Get the permission object so we can use it to assign permission to the sheet
            var permission = googleDriveManager.CreateUserPermission("reader", "*****@*****.**");
            if (permission == null)
            {
                Console.WriteLine("******* ERROR: Failed to create user permission object for [email protected] *******");
                Console.ReadKey();
                return;
            }

            // Assign the permission to the sheet
            if (googleDriveManager.AssignPermissionToFile(driveServiceFullAccess, sheetFileId, permission) == false)
            {
                Console.WriteLine("******* ERROR: Failed to set reader permission for user [email protected] for spreadsheet *******");
                Console.ReadKey();
                return;
            }

            // Get the permission object so we can use it to assign permission to the sheet
            permission = googleDriveManager.CreateUserPermission("writer", "*****@*****.**");
            if (permission == null)
            {
                Console.WriteLine("******* ERROR: Failed to create user permission object for [email protected] *******");
                Console.ReadKey();
                return;
            }

            // Assign the permission to the sheet
            if (googleDriveManager.AssignPermissionToFile(driveServiceFullAccess, sheetFileId, permission) == false)
            {
                Console.WriteLine("******* ERROR: Failed to set write permission for user [email protected] for spreadsheet *******");
                Console.ReadKey();
                return;
            }

            #endregion

            #region STEP 3: Get Sheets Service Instance

            #region STEP 3.1: Negative Testing

            // STEP 3.1.1 - Test no credentials passed in and no application name
            try { var expectedFailure = googleSheetManager.CreateSheetServiceInstance(null, string.Empty); }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 3.1.1 - PASSED : EXPECTED FAILURE with no credentials passed: ");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
            }

            // STEP 2.1.2 - Test credentials passed in but no application name
            try { var expectedFailure = googleSheetManager.CreateSheetServiceInstance(credentialReadOnly, string.Empty); }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 3.1.2 - PASSED : EXPECTED FAILURE with no application name passed: ");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
            }

            #endregion

            #region STEP 3.2: Positive Testing

            // STEP 3.2.1 - Test getting sheet service with ReadOnly Scope
            try
            {
                sheetsServiceReadOnly = googleSheetManager.CreateSheetServiceInstance(credentialReadOnly, "UnitTest Google Sheet Library");
                if (sheetsServiceReadOnly == null)
                {
                    Console.WriteLine("******* STEP 3.2.1 - FAILED - sheets service returned null getting it with Read Only credentials. ");
                    Console.WriteLine("*******");
                    Console.ReadKey();
                    return;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 3.2.1 - FAILED - Exception thrown getting sheets service using Read Only credentials.");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            // STEP 3.2.2 - Test getting sheet service with Full Access Scope
            try
            {
                sheetsServiceFullAccess = googleSheetManager.CreateSheetServiceInstance(credentialFullAccess, "UnitTest Google Sheet Library");
                if (sheetsServiceFullAccess == null)
                {
                    Console.WriteLine("******* STEP 3.2.2 - FAILED - sheets service returned null getting it with Full Access credentials. ");
                    Console.WriteLine("*******");
                    Console.ReadKey();
                    return;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("******* STEP 3.2.2 - FAILED - Exception thrown getting sheets service using Full Access credentials.");
                Console.WriteLine(exception.ToString());
                Console.WriteLine("*******");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("******* STEP 3.2 - PASSED");
            Console.WriteLine("*******");
            Console.ReadKey();

            #endregion

            #endregion

            #region STEP 4: Read and Write Values from test sheet

            #region STEP 4.1 - Read range (should be blank)

            var testRange    = "Sheet1!A2:E2";
            var valueResults = googleSheetManager.GetSheetDataRange(sheetsServiceReadOnly, sheetFileId, testRange);

            if (valueResults == null)
            {
                Console.WriteLine("******* ERROR: Failed to get range in sheet *******");
                Console.ReadKey();
                return;
            }

            // Review the cells are empty. The Values are stored in an array of arrays
            IList <IList <Object> > values = valueResults.Values;
            if (values == null || values.Count == 0)
            {
                Console.WriteLine("******* PASSED: Expected range in sheet to be empty as we just created it *******");
                Console.ReadKey();
            }
            else
            {
                Console.WriteLine("******* ERROR: Failed as sheet range should have no data *******");
                Console.ReadKey();
                return;
            }

            #endregion

            #region STEP 4.2 - Write a single value and confirm it got written

            var singleCellTestRange = "Sheet1!A2";
            googleSheetManager.UpdateSheetCellValue(sheetsServiceFullAccess, sheetFileId, singleCellTestRange, "New Text In A2");
            var singleCellValueResult = googleSheetManager.GetSheetDataRange(sheetsServiceReadOnly, sheetFileId, singleCellTestRange);
            if (singleCellValueResult == null || singleCellValueResult.Values.Count == 0)
            {
                Console.WriteLine("******* ERROR: Failed to read back single cell value *******");
                Console.ReadKey();
                return;
            }

            if (singleCellValueResult.Values[0].Count == 0)
            {
                Console.WriteLine("******* ERROR: Failed to read back single cell value *******");
                Console.ReadKey();
                return;
            }

            if (singleCellValueResult.Values[0][0].ToString() != "New Text In A2")
            {
                Console.WriteLine("******* ERROR: Failed as single text value was not updated in the sheet *******");
                Console.ReadKey();
                return;
            }
            #endregion

            #region STEP 5: Bulk Write data to test sheet

            // Getting the range again as we did update a cell in the range.
            valueResults = googleSheetManager.GetSheetDataRange(sheetsServiceReadOnly, sheetFileId, testRange);

            // Fill in values in the results
            for (int rowIndex = 0; rowIndex < valueResults.Values.Count; rowIndex++)
            {
                for (int colIndex = 0; colIndex < valueResults.Values[rowIndex].Count; colIndex++)
                {
                    valueResults.Values[rowIndex][colIndex] = rowIndex.ToString() + ":" + colIndex.ToString();
                }
            }

            // Write the values
            googleSheetManager.UpdateSheetDataRange(sheetsServiceFullAccess, sheetFileId, testRange, valueResults);

            // Getting the range again to confirm updates.
            valueResults = googleSheetManager.GetSheetDataRange(sheetsServiceReadOnly, sheetFileId, testRange);

            // Check the expected values in the results
            for (int rowIndex = 0; rowIndex < valueResults.Values.Count; rowIndex++)
            {
                for (int colIndex = 0; colIndex < valueResults.Values[rowIndex].Count; colIndex++)
                {
                    if (valueResults.Values[rowIndex][colIndex].ToString() != rowIndex.ToString() + ":" + colIndex.ToString())
                    {
                        Console.WriteLine("******* ERROR: Failed as the value we expected to be updated was not: " + rowIndex.ToString() + ":" + colIndex.ToString() + " * ******");
                        Console.ReadKey();
                        return;
                    }
                }
            }

            #endregion

            #endregion
        }
示例#22
0
        public static GoogleCredential GetServiceCredentialWithJwtFlag()
        {
            ServiceAccountCredential credential = (ServiceAccountCredential)s_serviceCredential.Value.UnderlyingCredential;

            return(GoogleCredential.FromServiceAccountCredential(credential.WithUseJwtAccessWithScopes(true)));
        }
示例#23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RecordingService"/> class.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="googleCredential">The google credential.</param>
 public RecordingService(LingDbContext context, GoogleCredential googleCredential)
 {
     _context          = context;
     _googleCredential = googleCredential;
 }
        public static IServiceCollection AddFtpServices(
            this IServiceCollection services,
            FtpOptions options)
        {
            services
            .Configure <AuthTlsOptions>(
                opt =>
            {
                opt.ServerCertificate = options.GetCertificate();
                opt.ImplicitFtps      = options.Ftps.Implicit;
            })
            .Configure <FtpConnectionOptions>(
                opt => opt.DefaultEncoding = Encoding.ASCII)
            .Configure <FubarDev.FtpServer.FtpServerOptions>(
                opt =>
            {
                opt.ServerAddress        = options.Server.Address;
                opt.Port                 = options.GetServerPort();
                opt.MaxActiveConnections = options.Server.MaxActiveConnections ?? 0;
                opt.ConnectionInactivityCheckInterval =
                    ToTimeSpan(options.Server.ConnectionInactivityCheckInterval);
            })
            .Configure <PortCommandOptions>(
                opt =>
            {
                if (options.Server.UseFtpDataPort)
                {
                    opt.DataPort = options.GetServerPort() - 1;
                }
            })
            .Configure <SimplePasvOptions>(
                opt =>
            {
                var portRange = options.GetPasvPortRange();
                if (portRange != null)
                {
                    (opt.PasvMinPort, opt.PasvMaxPort) = portRange.Value;
                }
            })
            .Configure <PasvCommandOptions>(opt => opt.PromiscuousPasv     = options.Server.Pasv.Promiscuous)
            .Configure <GoogleDriveOptions>(opt => opt.UseBackgroundUpload = options.GoogleDrive.BackgroundUpload)
            .Configure <FileSystemAmazonS3Options>(
                opt =>
            {
                opt.BucketName         = options.AmazonS3.BucketName;
                opt.BucketRegion       = options.AmazonS3.BucketRegion;
                opt.AwsAccessKeyId     = options.AmazonS3.AwsAccessKeyId;
                opt.AwsSecretAccessKey = options.AmazonS3.AwsSecretAccessKey;
            });
#if NETCOREAPP
            services
            .Configure <PamMembershipProviderOptions>(
                opt => opt.IgnoreAccountManagement = options.Pam.NoAccountManagement);
#endif

            // Add "Hello" service - unique per FTP connection
            services.AddScoped <Hello>();

            // Add custom command handlers
            services.AddSingleton <IFtpCommandHandlerScanner>(
                _ => new AssemblyFtpCommandHandlerScanner(typeof(HelloFtpCommandHandler).Assembly));

            // Add custom command handler extensions
            services.AddSingleton <IFtpCommandHandlerExtensionScanner>(
                sp => new AssemblyFtpCommandHandlerExtensionScanner(
                    sp.GetRequiredService <IFtpCommandHandlerProvider>(),
                    sp.GetService <ILogger <AssemblyFtpCommandHandlerExtensionScanner> >(),
                    typeof(SiteHelloFtpCommandHandlerExtension).Assembly));

#if NETCOREAPP
            if (options.SetFileSystemId && RuntimeEnvironment.OperatingSystemPlatform !=
                Microsoft.DotNet.PlatformAbstractions.Platform.Windows)
            {
                services.AddScoped <IFtpCommandMiddleware, FsIdChanger>();
            }
#endif

            switch (options.BackendType)
            {
            case FileSystemType.InMemory:
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseInMemoryFileSystem().ConfigureServer(options))
                           .Configure <InMemoryFileSystemOptions>(
                    opt => opt.KeepAnonymousFileSystem = options.InMemory.KeepAnonymous);
                break;

            case FileSystemType.Unix:
#if NETCOREAPP
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseUnixFileSystem().ConfigureServer(options))
                           .Configure <UnixFileSystemOptions>(
                    opt =>
                {
                    opt.Root            = options.Unix.Root;
                    opt.FlushAfterWrite = options.Unix.FlushAfterWrite;
                });
#else
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseDotNetFileSystem().ConfigureServer(options))
                           .Configure <DotNetFileSystemOptions>(
                    opt =>
                {
                    opt.RootPath        = options.Unix.Root;
                    opt.FlushAfterWrite = options.Unix.FlushAfterWrite;
                });
#endif
                break;

            case FileSystemType.SystemIO:
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseDotNetFileSystem().ConfigureServer(options))
                           .Configure <DotNetFileSystemOptions>(
                    opt =>
                {
                    opt.RootPath        = options.SystemIo.Root;
                    opt.FlushAfterWrite = options.SystemIo.FlushAfterWrite;
                });
                break;

            case FileSystemType.GoogleDriveUser:
                var userCredential = GetUserCredential(
                    options.GoogleDrive.User.ClientSecrets ?? throw new ArgumentNullException(
                        nameof(options.GoogleDrive.User.ClientSecrets),
                        "Client secrets file not specified."),
                    options.GoogleDrive.User.UserName ?? throw new ArgumentNullException(
                        nameof(options.GoogleDrive.User.ClientSecrets),
                        "User name not specified."),
                    options.GoogleDrive.User.RefreshToken);
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseGoogleDrive(userCredential).ConfigureServer(options));
                break;

            case FileSystemType.GoogleDriveService:
                var serviceCredential = GoogleCredential
                                        .FromFile(options.GoogleDrive.Service.CredentialFile)
                                        .CreateScoped(DriveService.Scope.Drive, DriveService.Scope.DriveFile);
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseGoogleDrive(serviceCredential).ConfigureServer(options));
                break;

            case FileSystemType.AmazonS3:
                services = services
                           .AddFtpServer(sb => sb.ConfigureAuthentication(options).UseS3FileSystem().ConfigureServer(options));
                break;

            default:
                throw new NotSupportedException(
                          $"Backend of type {options.Backend} cannot be run from configuration file options.");
            }

            switch (options.LayoutType)
            {
            case FileSystemLayoutType.SingleRoot:
                services.AddSingleton <IAccountDirectoryQuery, SingleRootWithoutHomeAccountDirectoryQuery>();
                break;

            case FileSystemLayoutType.PamHome:
#if NETCOREAPP
                services
                .AddSingleton <IAccountDirectoryQuery, PamAccountDirectoryQuery>()
                .Configure <PamAccountDirectoryQueryOptions>(
                    opt => opt.AnonymousRootDirectory = Path.GetTempPath());
                break;
#endif
            case FileSystemLayoutType.PamHomeChroot:
#if NETCOREAPP
                services
                .AddSingleton <IAccountDirectoryQuery, PamAccountDirectoryQuery>()
                .Configure <PamAccountDirectoryQueryOptions>(
                    opt =>
                {
                    opt.AnonymousRootDirectory = Path.GetTempPath();
                    opt.UserHomeIsRoot         = true;
                });
                break;
#endif
            case FileSystemLayoutType.RootPerUser:
                services
                .AddSingleton <IAccountDirectoryQuery, RootPerUserAccountDirectoryQuery>()
                .Configure <RootPerUserAccountDirectoryQueryOptions>(opt => opt.AnonymousRootPerEmail = true);
                break;
            }

#if NETCOREAPP
            services.Decorate <IFtpServer>(
                (ftpServer, serviceProvider) =>
            {
                /* Setting the umask is only valid for non-Windows platforms. */
                if (!string.IsNullOrEmpty(options.Umask) &&
                    RuntimeEnvironment.OperatingSystemPlatform !=
                    Microsoft.DotNet.PlatformAbstractions.Platform.Windows)
                {
                    var umask = options.Umask !.StartsWith("0")
                            ? Convert.ToInt32(options.Umask, 8)
                            : Convert.ToInt32(options.Umask, 10);

                    Syscall.umask((FilePermissions)umask);
                }

                return(ftpServer);
            });
#endif

            services.Scan(
                ts => ts
                .FromAssemblyOf <HostedFtpService>()
                .AddClasses(itf => itf.AssignableTo <IModuleInfo>(), true).As <IModuleInfo>()
                .WithSingletonLifetime());

            return(services);
        }
示例#25
0
        static void Main(string[] args)
        {
            using (var connection = new NpgsqlConnection("Host=127.0.0.1;port=5701;Username=arun;Password=V0zxKQd6M0Qp;Database=hiring"))

            {
                connection.Open();

                Console.WriteLine("\nEnter Table name ? ");
                var name = Console.ReadLine();

                NpgsqlCommand command = new NpgsqlCommand("Select * from " + name + " ", connection);

                var value           = connection.Query <string>("Select * from " + name + " ;");
                NpgsqlDataReader dr = command.ExecuteReader();

                FileStream   ostrm;
                StreamWriter writer;
                try
                {
                    ostrm  = new FileStream("C:/" + name + ".tsv", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    writer = new StreamWriter(ostrm);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Cannot open " + name + ".tsv for writing");
                    Console.WriteLine(e.Message);
                    return;
                }

                Console.SetOut(writer);
                while (dr.Read())
                {
                    Console.WriteLine("{0}\t{1}\t{2} \n", dr[0], dr[1], dr[2]);
                }

                writer.Close();
                ostrm.Close();

                Console.Read();
            }

            Console.Read();


            var credentialsPath  = "auth\\GCP.Client.Secret.Candidate.Task.json";
            var credentialsJson  = File.ReadAllText(credentialsPath);
            var googleCredential = GoogleCredential.FromJson(credentialsJson);
            var storageClient    = StorageClient.Create(googleCredential);

            storageClient.Service.HttpClient.Timeout = new TimeSpan(1, 0, 0);

            var fileInfo   = new FileInfo(credentialsPath);
            var fileStream = fileInfo.OpenRead();

            var bucketName = "gd-hiring-tri";

            storageClient.UploadObject(
                bucketName,
                "C:/film.tsv",
                "text/html",
                fileStream
                );

            Console.ReadLine();
        }
示例#26
0
        public static void executeProbes(string api)
        {
            StackdriverUtilClass util = new StackdriverUtilClass(api);
            GoogleCredential     auth = GoogleCredential.GetApplicationDefault();
            ClientBase           client;
            ProbeTestsBaseClass  test;

            System.Type type;

            Dictionary <string, string> probe_functions = new Dictionary <string, string>();

            if (api == "firestore")
            {
                Grpc.Core.Channel channel = new Grpc.Core.Channel(_FIRESTORE_TARGET, GoogleGrpcCredentials.ToChannelCredentials(auth));
                client          = new Firestore.FirestoreClient(channel);
                test            = new FirestoreProbesTestClass();
                probe_functions = (test as FirestoreProbesTestClass).GetProbFunctions();
                type            = typeof(FirestoreProbesTestClass);
            }
            else if (api == "spanner")
            {
                Grpc.Core.Channel channel = new Grpc.Core.Channel(_SPANNER_TARGET, GoogleGrpcCredentials.ToChannelCredentials(auth));
                client          = new Spanner.SpannerClient(channel);
                test            = new SpannerProbesTestClass();
                probe_functions = (test as SpannerProbesTestClass).GetProbFunctions();
                type            = typeof(SpannerProbesTestClass);
            }
            else
            {
                Console.WriteLine("grpc not implemented for {0}", api);
                return;
            }

            //object value = test.GetType().GetMethod("GetProbFunctions").Invoke(test,null);
            //probe_functions = value.GetType().GetProperties()

            int total   = probe_functions.Count;
            int success = 0;
            Dictionary <string, long> metrics = new Dictionary <string, long>();

            foreach (var probe in probe_functions)
            {
                MethodInfo fun        = type.GetMethod(probe.Value);
                object[]   parameters = new object[] { client, metrics };

                try
                {
                    if (api == "firestore")
                    {
                        fun.Invoke((test as FirestoreProbesTestClass), parameters);
                    }
                    else
                    {
                        fun.Invoke((test as SpannerProbesTestClass), parameters);
                    }

                    success++;
                }
                catch (Exception error)
                {
                    Console.WriteLine("{0}", error);
                    util.reportError(error);
                }
            }

            if (success == total)
            {
                util.setSuccess(true);
            }
            util.addMetrics(metrics);
            util.outputMetrics();

            if (success != total)
            {
                return;
            }
        }
        private static ISigner CreateTestSigner(string filePath)
        {
            var credential = GoogleCredential.FromFile(filePath);

            return(new ServiceAccountSigner(credential.ToServiceAccountCredential()));
        }
示例#28
0
        /// <summary>
        /// Authenticating to Google using a Service account
        /// Documentation: https://developers.google.com/accounts/docs/OAuth2#serviceaccount
        /// </summary>
        /// <param name="serviceAccountEmail">From Google Developer console https://console.developers.google.com</param>
        /// <param name="serviceAccountCredentialFilePath">Location of the .p12 or Json Service account key file downloaded from Google Developer console https://console.developers.google.com</param>
        /// <returns>AnalyticsService used to make requests against the Analytics API</returns>
        public static Cloud User AccountsService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath)
        {
            try
            {
                if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
                {
                    throw new Exception("Path to the service account credentials file is required.");
                }
                if (!File.Exists(serviceAccountCredentialFilePath))
                {
                    throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
                }
                if (string.IsNullOrEmpty(serviceAccountEmail))
                {
                    throw new Exception("ServiceAccountEmail is required.");
                }

                // These are the scopes of permissions you need. It is best to request only what you need and not all of them
                string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics };             // View your Google Analytics data

                // For Json file
                if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
                {
                    GoogleCredential credential;
                    using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
                    {
                        credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(scopes);
                    }

                    // Create the  Analytics service.
                    return(new Cloud User AccountsService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Cloud User Accounts Service account Authentication Sample",
                    }));
                }
                else if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".p12")
                {   // If its a P12 file
                    var certificate = new X509Certificate2(serviceAccountCredentialFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
                    var credential  = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = scopes
                    }.FromCertificate(certificate));

                    // Create the  Cloud User Accounts service.
                    return(new Cloud User AccountsService(new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Cloud User Accounts Authentication Sample",
                    }));
                }
                else
                {
                    throw new Exception("Unsupported Service accounts credentials.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create service account Cloud User AccountsService failed" + ex.Message);
                throw new Exception("CreateServiceAccountCloud User AccountsFailed", ex);
            }
        }
示例#29
0
        public static async Task MainFirebaseAdminAuth()
        {
            using (var firebase = new FireBase.Notification.Firebase())
            {
                await firebase.PushNotifyAsync(sender_Id, "Hello", "World");

                FileStream serviceAccount = new FileStream(path_to_private_key, FileMode.Open); // get the content of the file
                var        credential     = GoogleCredential.FromStream(serviceAccount);        // obtain the credential from the file
                serviceAccount.Close();                                                         //close the file (release the file handle, and possibly the thread!)
                var storage = StorageClient.Create(credential);                                 //storage details

                // Make an authenticated API request.
                //new FCMPushNotification().SendNotification("_title", "_message", "_topic", "deviceId");

                //firebase.PushNotifyAsync(id, "Hello", "World").Wait();
                var buckets = storage.ListBuckets(projectId);//Todo: check why the bucket was empty!
                //buckets
                foreach (var bucket in buckets)
                {
                    Console.WriteLine($"{bucket.Name}::{bucket}");
                }
                //FirebaseInstanceId.getInstance().getToken();
                //try
                //{
                //    FirebaseApp.Create(new AppOptions()
                //    {
                //        Credential = GoogleCredential.GetApplicationDefault(),
                //    });
                //}
                //catch (Exception ex)
                //{

                //}
                try
                {
                    var firebaseApp = FirebaseApp.Create(new AppOptions()
                    {
                        Credential = GoogleCredential.FromFile(path_to_private_key),
                    }, "My_Beautiful_App");
                    FirebaseMessaging.GetMessaging(firebaseApp);
                    new FCMPushNotification().SendNotification("title", "message", "topic", deviceId);
                    await new ProgramFirebaseAdminAuth().SendNotification(new List <string>()
                    {
                        deviceId, deviceId
                    }, "title", "body: some sort of msg to be sent to a device!");
                }
                catch (FirebaseException fex)
                {
                }
                catch (Exception ex)
                {
                }

                //try
                //{
                //    FirebaseApp.Create();
                //    // Initialize the default app
                //    var defaultApp = FirebaseApp.Create("defaultOptions");

                //    // Initialize another app with a different config
                //    var otherApp = FirebaseApp.Create("other");

                //    Console.WriteLine(defaultApp.Name); // "defaultOptions"
                //    Console.WriteLine(otherApp.Name); // "other"

                //    // Use the shorthand notation to retrieve the default app's services
                //    var defaultAuth = FirebaseAuth.DefaultInstance;

                //    // Use the otherApp variable to retrieve the other app's services
                //    var otherAuth = FirebaseAuth.GetAuth(otherApp);

                //}
                //catch (Exception ex)
                //{

                //}
            }
        }
示例#30
0
 /// <summary>
 /// Creates OAuth2 interceptor.
 /// </summary>
 public static MetadataInterceptorDelegate Create(GoogleCredential googleCredential)
 {
     var interceptor = new OAuth2Interceptor(googleCredential.InternalCredential, SystemClock.Default);
     return new MetadataInterceptorDelegate(interceptor.InterceptHeaders);
 }
示例#31
0
        static async Task Main(string[] args)
        {
            string importBucket = @"doc-processing-import-poc";
            string exportBucket = @"documentprocessing_export_poc";


            using (httpClient)
            {
                //httpClient.BaseAddress = new Uri("https://us-documentai.googleapis.com/v1beta3");
                httpClient.DefaultRequestHeaders.Add("X-Goog-User-Project", "docsprocessing-poc");
                httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue()
                {
                    NoCache = true, NoStore = true
                };

                Console.WriteLine("Hello World!");

                //await GoogleServiceAccountToken();

                var timestamp = Regex.Replace($"{DateTime.Now.ToShortDateString()}_{DateTime.Now.Hour}_{DateTime.Now.Minute}", @"[^\d]", "_");
                var dName     = $@"C:\2021_Local\OCR\Google Alpha\Beta3\Single Page Image Quality\{timestamp}";
                if (!Directory.Exists(dName))
                {
                    Directory.CreateDirectory(dName);
                }

                string TempBucketFolder = $@"KeithVoelsTest/{timestamp}";

                // Explicitly use service account credentials by specifying
                // the private key file.
                var credential = await GoogleCredential.FromFileAsync(serviceAccountJsonFile, CancellationToken.None);

                var storage = await StorageClient.CreateAsync(credential);

                //---------------------------------------------------------------------------------------------------------------
                // Upload files

                //await DeleteDocumentObjects(storage, importBucket, "Mock"); // Delete the files that already exist in temp folder
                //await DeleteDocumentObjects(storage, exportBucket, "Mock"); // Delete the files that already exist in temp folder

                //var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2", @"*.tif").Select(f => new FileInfo(f)).ToList();
                //files.AddRange(Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Split", @"*.tif").Select(f => new FileInfo(f)));

                //foreach (var file in files)
                //{
                //    await UploadDocumentObject(storage, importBucket, file.FullName, $"Mock/{file.Name}");
                //}

                //await CallDocumentApi_HttpClient(files.Select(f => ($"{TempBucketFolder}{f.Name}", $"{TempBucketFolder}{f.Name}")));

                //await DownloadDocumentObjects(storage, exportBucket, TempBucketFolder, dName);

                //---------------------------------------------------------------------------------------------------------------
                // Process file already in the storage bucket

                //await DeleteDocumentObjects(storage, exportBucket, "KeithVoelsTest"); // Delete the files that already exist


                //var documentObjects = await GetAllDocumentObjects(storage, importBucket, @"Google Pilot Test Docs Batch 2 Single Page/");
                //var documentObjects = await GetAllDocumentObjects(storage, importBucket, @"Google Pilot Test Docs Batch 2/");

                //var documentNames = documentObjects.Where(d => d.Name.EndsWith("tif")).Select(d => d.Name).ToList();

                //var directoryNames = Directory.GetDirectories($@"C:\2021_Local\OCR\Google Alpha\Beta3\Mixed\", "*", SearchOption.AllDirectories).Select(d => new DirectoryInfo(d)).Select(d => d.Name).ToList();

                //////// Split and Classify
                //foreach (var f in documentNames)
                //{
                //    var shortName = Path.GetFileNameWithoutExtension(f);

                //    if (!directoryNames.Contains(shortName))
                //    {
                //        var pdr = new BatchRequest($"gs://doc-processing-import-poc/{f}", $"gs://documentprocessing_export_poc/{$"{TempBucketFolder}/{shortName}"}");
                //        await CallDocumentApi_HttpClient_BatchProcess(pdr);
                //    }
                //}

                //await DownloadDocumentObjects(storage, exportBucket, "KeithVoelsTest", dName);

                //File.WriteAllText($@"{dName}\Summary.txt", string.Join('\n', Log.Select(l => l.ToString())));

                //---------------------------------------------------------------------------------------------------------------
                // Extract

                //var fileNames = new List<(string FileName, string classification)>()
                //{
                //    ("3459817085_21_2635078027_V_2", "w2_2019")
                //};

                var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\SinglePage", @"*.tif").Select(f => new FileInfo(f)).ToList();

                //var files = documentNames.Select(x => new FileInfo($@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Form1040\{x}"));

                //var files = Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2", @"*.tif").Select(f => new FileInfo(f)).ToList();
                //files.AddRange(Directory.GetFiles(@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Split", @"*.tif").Select(f => new FileInfo(f)));


                foreach (var doc in files)
                {
                    //var doc = new FileInfo($@"C:\2021_Local\OCR\Documents\Google Pilot Test Docs Batch 2\Visually Split\{fileName.FileName}.tif");

                    if (!doc.Exists)
                    {
                        throw new FileNotFoundException();
                    }
                    var pdr = new Request(doc);
                    try
                    {
                        //var type = DocumentType.Form1040;

                        //if (fileName.classification.Contains("w2"))
                        //{
                        //    type = DocumentType.W2;
                        //}

                        var json = await CallDocumentApi_HttpClient_Process(pdr, DocumentType.ImageQuality);

                        var result = new FileInfo($@"{dName}\{doc.Name}");
                        if (!result.Directory.Exists)
                        {
                            result.Directory.Create();
                        }
                        await File.WriteAllTextAsync($@"{dName}\{ Path.GetFileNameWithoutExtension(result.Name) }.json", json);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Exceptions: {ex.Message}");
                        await File.WriteAllTextAsync($@"{dName}\{Path.GetFileNameWithoutExtension(doc.Name)}.json", ex.Message);
                    }
                }


                //---------------------------------------------------------------------------------------------------------------
                // Process file already in the storage bucket
                // One Off - Download Documents

                //await DownloadDocumentObjects(storage, exportBucket, @"KeithVoelsTest", dName);
            }
        }